下面列出了怎么用org.eclipse.lsp4j.DocumentRangeFormattingParams的API类实例代码及写法,或者点击链接到github查看源代码。
private void rangeFormat(FileObject fo, LSPBindings bindings) throws BadLocationException {
DocumentRangeFormattingParams drfp = new DocumentRangeFormattingParams();
drfp.setTextDocument(new TextDocumentIdentifier(Utils.toURI(fo)));
drfp.setOptions(new FormattingOptions(
IndentUtils.indentLevelSize(ctx.document()),
IndentUtils.isExpandTabs(ctx.document())));
drfp.setRange(new Range(
Utils.createPosition(ctx.document(), ctx.startOffset()),
Utils.createPosition(ctx.document(), ctx.endOffset())));
List<TextEdit> edits = new ArrayList<>();
try {
edits = new ArrayList<>(bindings.getTextDocumentService().rangeFormatting(drfp).get());
} catch (InterruptedException | ExecutionException ex) {
LOG.log(Level.INFO,
String.format("LSP document rangeFormat failed for {0}", fo),
ex);
}
applyTextEdits(edits);
}
protected void testRangeFormatting(final Procedure1<? super DocumentRangeFormattingParams> paramsConfigurator, final Procedure1<? super RangeFormattingConfiguration> configurator) {
try {
@Extension
final RangeFormattingConfiguration configuration = new RangeFormattingConfiguration();
configuration.setFilePath(("MyModel." + this.fileExtension));
configurator.apply(configuration);
final FileInfo fileInfo = this.initializeContext(configuration);
DocumentRangeFormattingParams _documentRangeFormattingParams = new DocumentRangeFormattingParams();
final Procedure1<DocumentRangeFormattingParams> _function = (DocumentRangeFormattingParams it) -> {
String _uri = fileInfo.getUri();
TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(_uri);
it.setTextDocument(_textDocumentIdentifier);
it.setRange(configuration.getRange());
if ((paramsConfigurator != null)) {
paramsConfigurator.apply(it);
}
};
DocumentRangeFormattingParams _doubleArrow = ObjectExtensions.<DocumentRangeFormattingParams>operator_doubleArrow(_documentRangeFormattingParams, _function);
final CompletableFuture<List<? extends TextEdit>> changes = this.languageServer.rangeFormatting(_doubleArrow);
String _contents = fileInfo.getContents();
final Document result = new Document(Integer.valueOf(1), _contents).applyChanges(ListExtensions.<TextEdit>reverse(CollectionLiterals.<TextEdit>newArrayList(((TextEdit[])Conversions.unwrapArray(changes.get(), TextEdit.class)))));
this.assertEqualsStricter(configuration.getExpectedText(), result.getContents());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
/**
* Reformat the text currently selected in the editor
*/
public void reformatSelection() {
pool(() -> {
if (editor.isDisposed()) {
return;
}
DocumentRangeFormattingParams params = new DocumentRangeFormattingParams();
params.setTextDocument(identifier);
SelectionModel selectionModel = editor.getSelectionModel();
int start = computableReadAction(selectionModel::getSelectionStart);
int end = computableReadAction(selectionModel::getSelectionEnd);
Position startingPos = DocumentUtils.offsetToLSPPos(editor, start);
Position endPos = DocumentUtils.offsetToLSPPos(editor, end);
params.setRange(new Range(startingPos, endPos));
// Todo - Make Formatting Options configurable
FormattingOptions options = new FormattingOptions();
params.setOptions(options);
CompletableFuture<List<? extends TextEdit>> request = requestManager.rangeFormatting(params);
if (request == null) {
return;
}
request.thenAccept(formatting -> {
if (formatting == null) {
return;
}
invokeLater(() -> {
if (!editor.isDisposed()) {
applyEdit((List<TextEdit>) formatting, "Reformat selection", false);
}
});
});
});
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
return computeAsync((cancelChecker) -> {
String uri = params.getTextDocument().getUri();
TextDocument document = getDocument(uri);
CompositeSettings settings = new CompositeSettings(getSharedSettings(), params.getOptions());
return getXMLLanguageService().format(document, params.getRange(), settings);
});
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
URI uri = getURI(params.getTextDocument());
return openFilesManager.runInOpenFileContext(uri, "rangeFormatting", (ofc, ci) -> {
return rangeFormatting(ofc, params, ci);
});
}
/**
* Create the text edits for the formatter. Executed in a read request.
*/
protected List<? extends TextEdit> rangeFormatting(OpenFileContext ofc, DocumentRangeFormattingParams params,
CancelIndicator cancelIndicator) {
URI uri = ofc.getURI();
FormattingService formatterService = getService(uri, FormattingService.class);
if ((formatterService == null)) {
return Collections.emptyList();
}
XtextResource res = ofc.getResource();
XDocument doc = ofc.getDocument();
return formatterService.format(doc, res, params, cancelIndicator);
}
@Test
public void testRangeFormatting() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
//@formatter:off
"package org.sample;\n" +
" public class Baz {\n"+
"\tvoid foo(){\n" +
" }\n"+
" }\n"
//@formatter:on
);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Range range = new Range(new Position(2, 0), new Position(3, 5));// range around foo()
DocumentRangeFormattingParams params = new DocumentRangeFormattingParams(range);
params.setTextDocument(textDocument);
params.setOptions(new FormattingOptions(3, true));// ident == 3 spaces
List<? extends TextEdit> edits = server.rangeFormatting(params).get();
//@formatter:off
String expectedText =
"package org.sample;\n" +
" public class Baz {\n"+
" void foo() {\n" +
" }\n"+
" }\n";
//@formatter:on
String newText = TextEditUtil.apply(unit, edits);
assertEquals(expectedText, newText);
}
/**
* Create the text edits for the formatter. Executed in a read request.
* @since 2.20
*/
protected List<? extends TextEdit> rangeFormatting(DocumentRangeFormattingParams params,
CancelIndicator cancelIndicator) {
URI uri = getURI(params.getTextDocument());
FormattingService formatterService = getService(uri, FormattingService.class);
if (formatterService == null) {
return Collections.emptyList();
}
return workspaceManager.doRead(uri,
(document, resource) -> formatterService.format(document, resource, params, cancelIndicator));
}
public List<? extends TextEdit> format(Document document, XtextResource resource,
DocumentRangeFormattingParams params, CancelIndicator cancelIndicator) {
int startOffset = document.getOffSet(params.getRange().getStart());
int endOffset = document.getOffSet(params.getRange().getEnd());
int length = endOffset - startOffset;
return format(resource, document, startOffset, length, params.getOptions());
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
LOGGER.info("rangeFormatting: {}", params.getTextDocument());
return CompletableFuture.completedFuture(Collections.emptyList());
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
LOGGER.debug("rangeFormatting: {}", params.getTextDocument());
return CompletableFuture.completedFuture(Collections.emptyList());
}
List<? extends org.eclipse.lsp4j.TextEdit> rangeFormatting(DocumentRangeFormattingParams params, IProgressMonitor monitor) {
return format(params.getTextDocument().getUri(), params.getOptions(), params.getRange(), monitor);
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
logInfo(">> document/rangeFormatting");
FormatterHandler handler = new FormatterHandler(preferenceManager);
return computeAsync((monitor) -> handler.rangeFormatting(params, monitor));
}
/**
* This feature is not implemented at this time.
*/
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params)
{
return CompletableFuture.completedFuture(Collections.emptyList());
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
return requestManager.runRead((cancelIndicator) -> rangeFormatting(params, cancelIndicator));
}
/**
* The document range formatting request is sent from the client to the
* server to format a given range in a document.
*
* Registration Options: TextDocumentRegistrationOptions
*/
@JsonRequest
default CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
throw new UnsupportedOperationException();
}