-
Notifications
You must be signed in to change notification settings - Fork 12
Parametric: formatting #677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
630ccc4
d3675c4
b32d2f6
f16e17a
b749dcf
f1effe1
6fc3acf
bdb7989
a985a81
0196d07
d1c4e3e
49b66d0
998a115
551fe2a
84c5a35
c4b3561
1748678
1d56010
20a71ae
cda1531
206a57a
6cdab51
05b2955
9561883
7b92033
244da24
69307a7
c1b08a8
6f5657f
49cf9be
b23a5ea
ce46294
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,12 +62,15 @@ | |
| import org.eclipse.lsp4j.DidCloseTextDocumentParams; | ||
| import org.eclipse.lsp4j.DidOpenTextDocumentParams; | ||
| import org.eclipse.lsp4j.DidSaveTextDocumentParams; | ||
| import org.eclipse.lsp4j.DocumentFormattingParams; | ||
| import org.eclipse.lsp4j.DocumentRangeFormattingParams; | ||
| import org.eclipse.lsp4j.DocumentSymbol; | ||
| import org.eclipse.lsp4j.DocumentSymbolParams; | ||
| import org.eclipse.lsp4j.ExecuteCommandOptions; | ||
| import org.eclipse.lsp4j.FileRename; | ||
| import org.eclipse.lsp4j.FoldingRange; | ||
| import org.eclipse.lsp4j.FoldingRangeRequestParams; | ||
| import org.eclipse.lsp4j.FormattingOptions; | ||
| import org.eclipse.lsp4j.Hover; | ||
| import org.eclipse.lsp4j.HoverParams; | ||
| import org.eclipse.lsp4j.ImplementationParams; | ||
|
|
@@ -100,6 +103,7 @@ | |
| import org.eclipse.lsp4j.TextDocumentIdentifier; | ||
| import org.eclipse.lsp4j.TextDocumentItem; | ||
| import org.eclipse.lsp4j.TextDocumentSyncKind; | ||
| import org.eclipse.lsp4j.TextEdit; | ||
| import org.eclipse.lsp4j.VersionedTextDocumentIdentifier; | ||
| import org.eclipse.lsp4j.WorkspaceEdit; | ||
| import org.eclipse.lsp4j.WorkspaceFolder; | ||
|
|
@@ -110,6 +114,7 @@ | |
| import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode; | ||
| import org.eclipse.lsp4j.services.LanguageClient; | ||
| import org.eclipse.lsp4j.services.LanguageClientAware; | ||
| import org.eclipse.lsp4j.util.Ranges; | ||
| import org.rascalmpl.uri.URIResolverRegistry; | ||
| import org.rascalmpl.values.IRascalValueFactory; | ||
| import org.rascalmpl.values.parsetrees.ITree; | ||
|
|
@@ -236,6 +241,8 @@ public void initializeServerCapabilities(ServerCapabilities result) { | |
| result.setCodeLensProvider(new CodeLensOptions(false)); | ||
| result.setRenameProvider(new RenameOptions(true)); | ||
| result.setExecuteCommandProvider(new ExecuteCommandOptions(Collections.singletonList(getRascalMetaCommandName()))); | ||
| result.setDocumentFormattingProvider(true); | ||
| result.setDocumentRangeFormattingProvider(true); | ||
| result.setInlayHintProvider(true); | ||
| result.setSelectionRangeProvider(true); | ||
| result.setFoldingRangeProvider(true); | ||
|
|
@@ -407,7 +414,7 @@ private CompletableFuture<ISourceLocation> computeRenameRange(final ILanguageCon | |
| public CompletableFuture<WorkspaceEdit> rename(RenameParams params) { | ||
| logger.trace("rename for: {}, new name: {}", params.getTextDocument().getUri(), params.getNewName()); | ||
| final ILanguageContributions contribs = contributions(params.getTextDocument()); | ||
| final Position rascalPos = Locations.toRascalPosition(params.getTextDocument(), params.getPosition(), columns);; | ||
| final Position rascalPos = Locations.toRascalPosition(params.getTextDocument(), params.getPosition(), columns); | ||
| return getFile(params.getTextDocument()) | ||
| .getCurrentTreeAsync() | ||
| .thenApply(Versioned::get) | ||
|
|
@@ -727,6 +734,67 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio | |
| return CodeActions.mergeAndConvertCodeActions(this, dedicatedLanguageName, contribs.getName(), quickfixes, codeActions); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) { | ||
| logger.debug("Formatting: {}", params); | ||
|
|
||
| TextDocumentIdentifier uri = params.getTextDocument(); | ||
| final ILanguageContributions contribs = contributions(uri); | ||
|
|
||
| // call the `formatting` implementation of the relevant language contribution | ||
| return getFile(uri) | ||
| .getCurrentTreeAsync() | ||
| .thenApply(Versioned::get) | ||
| .thenCompose(tree -> { | ||
| final var opts = getFormattingOptions(params.getOptions()); | ||
| return contribs.formatting(VF.list(tree), opts).get(); | ||
| }) | ||
| .thenApply(l -> DocumentChanges.translateTextEdits(this, l, Map.of())); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) { | ||
| logger.debug("Formatting range: {}", params); | ||
|
|
||
| TextDocumentIdentifier uri = params.getTextDocument(); | ||
| Range range = params.getRange(); | ||
| final ILanguageContributions contribs = contributions(uri); | ||
|
|
||
| // call the `formatting` implementation of the relevant language contribution | ||
| var fileState = getFile(uri); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| return fileState | ||
| .getCurrentTreeAsync() | ||
| .thenApply(Versioned::get) | ||
| .thenCompose(tree -> { | ||
| // just a range | ||
| var start = Locations.toRascalPosition(uri, range.getStart(), columns); | ||
| var end = Locations.toRascalPosition(uri, range.getEnd(), columns); | ||
| // compute the focus list at the end of the range | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this comment is not accurate? |
||
| var focus = TreeSearch.computeFocusList(tree, start.getLine(), start.getCharacter(), end.getLine(), end.getCharacter()); | ||
|
|
||
| var opts = getFormattingOptions(params.getOptions()); | ||
| return contribs.formatting(focus, opts).get(); | ||
| }) | ||
| // convert the document changes | ||
| .thenApply(l -> DocumentChanges.translateTextEdits(this, l, Map.of()) | ||
| .stream() | ||
| .filter(e -> Ranges.containsRange(range, e.getRange())) | ||
| .collect(Collectors.toList())); | ||
| } | ||
|
|
||
| private IConstructor getFormattingOptions(FormattingOptions options) { | ||
| var optionsType = tf.abstractDataType(typeStore, "FormattingOptions"); | ||
| var consType = tf.constructor(typeStore, optionsType, "formattingOptions"); | ||
|
Comment on lines
+786
to
+787
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be stored in a field, and only looked-up at the start? |
||
| var opts = Map.of( | ||
| "tabSize", VF.integer(options.getTabSize()), | ||
| "insertSpaces", VF.bool(options.isInsertSpaces()), | ||
| "trimTrailingWhitespace", VF.bool(options.isTrimTrailingWhitespace()), | ||
| "insertFinalNewline", VF.bool(options.isInsertFinalNewline()), | ||
| "trimFinalNewlines", VF.bool(options.isTrimFinalNewlines()) | ||
| ); | ||
| return VF.constructor(consType, new IValue[0], opts); | ||
| } | ||
|
|
||
| private CompletableFuture<IList> computeCodeActions(final ILanguageContributions contribs, final int startLine, final int startColumn, ITree tree) { | ||
| IList focus = TreeSearch.computeFocusList(tree, startLine, startColumn); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this change in the pom.xml has to do with this PR?