下面列出了怎么用org.eclipse.lsp4j.WillSaveTextDocumentParams的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testWillSaveWaitUntil() throws Exception {
URI srcUri = project.getFile("src/java/Foo4.java").getRawLocationURI();
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(srcUri);
StringBuilder buf = new StringBuilder();
buf.append("package java;\n");
buf.append("\n");
buf.append("public class Foo4 {\n");
buf.append("}\n");
WillSaveTextDocumentParams params = new WillSaveTextDocumentParams();
TextDocumentIdentifier document = new TextDocumentIdentifier();
document.setUri(srcUri.toString());
params.setTextDocument(document);
List<TextEdit> result = handler.willSaveWaitUntil(params, monitor);
Document doc = new Document();
doc.set(cu.getSource());
assertEquals(TextEditUtil.apply(doc, result), buf.toString());
}
/**
* Indicates that the document will be saved
*/
//TODO Manual
public void willSave() {
if (wrapper.isWillSaveWaitUntil() && !needSave) {
willSaveWaitUntil();
} else
pool(() -> {
if (!editor.isDisposed()) {
requestManager.willSave(new WillSaveTextDocumentParams(identifier, TextDocumentSaveReason.Manual));
}
});
}
public List<TextEdit> willSaveWaitUntil(WillSaveTextDocumentParams params, IProgressMonitor monitor) {
List<TextEdit> edit = new ArrayList<>();
if (monitor.isCanceled()) {
return edit;
}
String documentUri = params.getTextDocument().getUri();
if (preferenceManager.getPreferences().isJavaSaveActionsOrganizeImportsEnabled()) {
edit.addAll(handleSaveActionOrganizeImports(documentUri, monitor));
}
return edit;
}
@Test
public void testStaticWillSaveWaitUntil() throws Exception {
String[] favourites = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getJavaCompletionFavoriteMembers();
try {
List<String> list = new ArrayList<>();
list.add("java.lang.Math.*");
list.add("java.util.stream.Collectors.*");
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setJavaCompletionFavoriteMembers(list);
URI srcUri = project.getFile("src/org/sample/Foo6.java").getRawLocationURI();
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(srcUri);
/* @formatter:off */
String expected = "package org.sample;\n" +
"\n" +
"import static java.lang.Math.PI;\n" +
"import static java.lang.Math.abs;\n" +
"import static java.util.stream.Collectors.toList;\n" +
"\n" +
"import java.util.List;\n" +
"\n" +
"public class Foo6 {\n" +
" List list = List.of(1).stream().collect(toList());\n" +
" double i = abs(-1);\n" +
" double pi = PI;\n" +
"}\n";
//@formatter:on
WillSaveTextDocumentParams params = new WillSaveTextDocumentParams();
TextDocumentIdentifier document = new TextDocumentIdentifier();
document.setUri(srcUri.toString());
params.setTextDocument(document);
List<TextEdit> result = handler.willSaveWaitUntil(params, monitor);
Document doc = new Document();
doc.set(cu.getSource());
assertEquals(expected, TextEditUtil.apply(doc, result));
} finally {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setJavaCompletionFavoriteMembers(Arrays.asList(favourites));
}
}
@Override
public CompletableFuture<List<TextEdit>> willSaveWaitUntil(WillSaveTextDocumentParams params) {
logInfo(">> document/willSaveWaitUntil");
SaveActionHandler handler = new SaveActionHandler(preferenceManager);
return computeAsync((monitor) -> handler.willSaveWaitUntil(params, monitor));
}
/**
* The document will save notification is sent from the client to the server before the document is actually saved.
*
* Registration Options: TextDocumentRegistrationOptions
*/
@JsonNotification
default void willSave(WillSaveTextDocumentParams params) {}
/**
* The document will save request is sent from the client to the server before the document is actually saved.
* The request can return an array of TextEdits which will be applied to the text document before it is saved.
* Please note that clients might drop results if computing the text edits took too long or if a server constantly fails on this request.
* This is done to keep the save fast and reliable.
*
* Registration Options: TextDocumentRegistrationOptions
*/
@JsonRequest
default CompletableFuture<List<TextEdit>> willSaveWaitUntil(WillSaveTextDocumentParams params) {
throw new UnsupportedOperationException();
}