|
60 | 60 | import org.eclipse.lsp4j.DidOpenTextDocumentParams; |
61 | 61 | import org.eclipse.lsp4j.DidSaveTextDocumentParams; |
62 | 62 | import org.eclipse.lsp4j.DocumentFormattingParams; |
| 63 | +import org.eclipse.lsp4j.DocumentRangeFormattingParams; |
63 | 64 | import org.eclipse.lsp4j.DocumentSymbol; |
64 | 65 | import org.eclipse.lsp4j.DocumentSymbolParams; |
65 | 66 | import org.eclipse.lsp4j.ExecuteCommandOptions; |
|
75 | 76 | import org.eclipse.lsp4j.Location; |
76 | 77 | import org.eclipse.lsp4j.LocationLink; |
77 | 78 | import org.eclipse.lsp4j.Position; |
| 79 | +import org.eclipse.lsp4j.Range; |
78 | 80 | import org.eclipse.lsp4j.ReferenceParams; |
79 | 81 | import org.eclipse.lsp4j.SelectionRange; |
80 | 82 | import org.eclipse.lsp4j.SelectionRangeParams; |
|
100 | 102 | import org.rascalmpl.uri.URIResolverRegistry; |
101 | 103 | import org.rascalmpl.values.IRascalValueFactory; |
102 | 104 | import org.rascalmpl.values.parsetrees.ITree; |
| 105 | +import org.rascalmpl.values.parsetrees.TreeAdapter; |
103 | 106 | import org.rascalmpl.vscode.lsp.BaseWorkspaceService; |
104 | 107 | import org.rascalmpl.vscode.lsp.IBaseLanguageClient; |
105 | 108 | import org.rascalmpl.vscode.lsp.IBaseTextDocumentService; |
@@ -531,17 +534,32 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio |
531 | 534 |
|
532 | 535 | @Override |
533 | 536 | public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) { |
534 | | - logger.debug("formatting: {}", params); |
| 537 | + logger.debug("Formatting: {}", params); |
| 538 | + return format(params.getTextDocument(), null, params.getOptions()); |
| 539 | + } |
535 | 540 |
|
536 | | - final ILanguageContributions contribs = contributions(params.getTextDocument()); |
| 541 | + @Override |
| 542 | + public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) { |
| 543 | + logger.debug("Formatting range: {}", params); |
| 544 | + return format(params.getTextDocument(), params.getRange(), params.getOptions()); |
| 545 | + } |
| 546 | + |
| 547 | + private CompletableFuture<List<? extends TextEdit>> format(TextDocumentIdentifier uri, @Nullable Range range, FormattingOptions options) { |
| 548 | + final ILanguageContributions contribs = contributions(uri); |
537 | 549 |
|
538 | 550 | // convert the `FormattingOptions` map to a `set[FormattingOption]` |
539 | | - ISet optSet = getFormattingOptions(params.getOptions()); |
| 551 | + ISet optSet = getFormattingOptions(options); |
540 | 552 | // call the `formatting` implementation of the relevant language contribution |
541 | | - return getFile(params.getTextDocument()) |
| 553 | + return getFile(uri) |
542 | 554 | .getCurrentTreeAsync() |
543 | 555 | .thenApply(Versioned::get) |
544 | | - .thenCompose(tree -> contribs.formatting(tree, optSet).get()) |
| 556 | + .thenCompose(tree -> { |
| 557 | + // range to Rascal loc |
| 558 | + ISourceLocation loc = range == null |
| 559 | + ? TreeAdapter.getLocation(tree) |
| 560 | + : null; // TODO map Range to ISourceLocation |
| 561 | + return contribs.formatting(tree, loc, optSet).get(); |
| 562 | + }) |
545 | 563 | // convert the document changes |
546 | 564 | .thenApply(l -> DocumentChanges.translateTextEdits(this, l, Map.of())); |
547 | 565 | } |
|
0 commit comments