org.eclipse.lsp4j.DocumentRangeFormattingParams Java Examples

The following examples show how to use org.eclipse.lsp4j.DocumentRangeFormattingParams. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Formatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: AbstractLanguageServerTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #3
Source File: XMLTextDocumentService.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@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);
	});
}
 
Example #4
Source File: FormattingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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());
}
 
Example #5
Source File: XLanguageServerImpl.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@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);
	});
}
 
Example #6
Source File: XLanguageServerImpl.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #7
Source File: LanguageServerImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example #8
Source File: EditorEventManager.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
                }
            });
        });
    });
}
 
Example #9
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example #10
Source File: FormatterHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
List<? extends org.eclipse.lsp4j.TextEdit> rangeFormatting(DocumentRangeFormattingParams params, IProgressMonitor monitor) {
	return format(params.getTextDocument().getUri(), params.getOptions(), params.getRange(), monitor);
}
 
Example #11
Source File: LanguageServerImpl.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
	return requestManager.runRead((cancelIndicator) -> rangeFormatting(params, cancelIndicator));
}
 
Example #12
Source File: ActionScriptServices.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
/**
 * This feature is not implemented at this time.
 */
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params)
{
    return CompletableFuture.completedFuture(Collections.emptyList());
}
 
Example #13
Source File: JDTLanguageServer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
	logInfo(">> document/rangeFormatting");
	FormatterHandler handler = new FormatterHandler(preferenceManager);
	return computeAsync((monitor) -> handler.rangeFormatting(params, monitor));
}
 
Example #14
Source File: TeiidDdlTextDocumentService.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
    LOGGER.debug("rangeFormatting: {}", params.getTextDocument());
    return CompletableFuture.completedFuture(Collections.emptyList());
}
 
Example #15
Source File: TextDocumentServiceImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams arg0) {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example #16
Source File: CamelTextDocumentService.java    From camel-language-server with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
	LOGGER.info("rangeFormatting: {}", params.getTextDocument());
	return CompletableFuture.completedFuture(Collections.emptyList());
}
 
Example #17
Source File: TextDocumentService.java    From lsp4j with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * 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();
}