Skip to content

Commit bac08fb

Browse files
committed
Add rangeFormatting, reuse formatting.
1 parent 4aca661 commit bac08fb

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/parametric/ILanguageContributions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public interface ILanguageContributions {
6060
public InterruptibleFuture<ISet> implementation(IList focus);
6161
public InterruptibleFuture<IList> codeAction(IList focus);
6262
public InterruptibleFuture<IList> selectionRange(IList focus);
63-
public InterruptibleFuture<IList> formatting(ITree input, ISet formattingOptions);
63+
public InterruptibleFuture<IList> formatting(ITree input, ISourceLocation loc, ISet formattingOptions);
6464

6565
public CompletableFuture<IList> parseCodeActions(String command);
6666

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/parametric/ParametricTextDocumentService.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
6161
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
6262
import org.eclipse.lsp4j.DocumentFormattingParams;
63+
import org.eclipse.lsp4j.DocumentRangeFormattingParams;
6364
import org.eclipse.lsp4j.DocumentSymbol;
6465
import org.eclipse.lsp4j.DocumentSymbolParams;
6566
import org.eclipse.lsp4j.ExecuteCommandOptions;
@@ -75,6 +76,7 @@
7576
import org.eclipse.lsp4j.Location;
7677
import org.eclipse.lsp4j.LocationLink;
7778
import org.eclipse.lsp4j.Position;
79+
import org.eclipse.lsp4j.Range;
7880
import org.eclipse.lsp4j.ReferenceParams;
7981
import org.eclipse.lsp4j.SelectionRange;
8082
import org.eclipse.lsp4j.SelectionRangeParams;
@@ -100,6 +102,7 @@
100102
import org.rascalmpl.uri.URIResolverRegistry;
101103
import org.rascalmpl.values.IRascalValueFactory;
102104
import org.rascalmpl.values.parsetrees.ITree;
105+
import org.rascalmpl.values.parsetrees.TreeAdapter;
103106
import org.rascalmpl.vscode.lsp.BaseWorkspaceService;
104107
import org.rascalmpl.vscode.lsp.IBaseLanguageClient;
105108
import org.rascalmpl.vscode.lsp.IBaseTextDocumentService;
@@ -531,17 +534,32 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
531534

532535
@Override
533536
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+
}
535540

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);
537549

538550
// convert the `FormattingOptions` map to a `set[FormattingOption]`
539-
ISet optSet = getFormattingOptions(params.getOptions());
551+
ISet optSet = getFormattingOptions(options);
540552
// call the `formatting` implementation of the relevant language contribution
541-
return getFile(params.getTextDocument())
553+
return getFile(uri)
542554
.getCurrentTreeAsync()
543555
.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+
})
545563
// convert the document changes
546564
.thenApply(l -> DocumentChanges.translateTextEdits(this, l, Map.of()));
547565
}

rascal-lsp/src/main/rascal/library/demo/lang/pico/LanguageServer.rsc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import util::ParseErrorRecovery;
4141
import util::Reflective;
4242
import lang::pico::\syntax::Main;
4343
import IO;
44+
import Location;
4445
import String;
4546

4647
import lang::box::\syntax::Box;
@@ -70,14 +71,13 @@ set[LanguageService] picoLanguageServer(bool allowRecovery) = {
7071
formatting(picoFormattingService)
7172
};
7273

73-
list[TextEdit] picoFormattingService(Tree input, FormattingOptions opts) {
74-
// pico tree to box formatting representation
74+
list[TextEdit] picoFormattingService(Tree input, loc range, FormattingOptions opts) {
7575
str original = "<input>";
76-
print("[original]");
77-
rprintln(original);
7876

77+
// pico tree to box formatting representation
7978
box = toBox(input);
8079
box = visit (box) { case i:I(_) => i[is=opts.tabSize] };
80+
8181
// box to string
8282
formatted = format(box);
8383

@@ -109,7 +109,8 @@ list[TextEdit] picoFormattingService(Tree input, FormattingOptions opts) {
109109
// computelayout differences as edits, and restore comments
110110
edits = layoutDiff(input, parse(#start[Program], formatted));
111111

112-
return edits;
112+
// TODO Instead of computing all edits and filtering, we can be more efficient by only formatting certain trees.
113+
return [e | e <- edits, isContainedIn(e.range, range)];
113114
}
114115
115116
Box toBox((Program) `begin <Declarations decls> <{Statement ";"}* body> end`, FormatOptions opts = formatOptions())

rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ data LanguageService
270270
| implementation(set[loc] (Focus _focus) implementationService)
271271
| codeAction (list[CodeAction] (Focus _focus) codeActionService)
272272
| selectionRange(list[loc](Focus _focus) selectionRangeService)
273-
| formatting (list[TextEdit](Tree _input, FormattingOptions _opts) formattingService)
273+
| formatting (list[TextEdit](Tree _input, loc range, FormattingOptions _opts) formattingService)
274274
;
275275

276276
data FormattingOptions(

0 commit comments

Comments
 (0)