Skip to content

Commit e36e251

Browse files
authored
Merge pull request #651 from usethesource/feature/rename-refactoring/language-server-api
Design LSP rename API
2 parents 3de370b + d71c3db commit e36e251

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ set[LanguageService] picoLanguageServer() = {
5252
execution(picoExecutionService),
5353
inlayHint(picoInlayHintService),
5454
definition(picoDefinitionService),
55-
codeAction(picoCodeActionService)
55+
codeAction(picoCodeActionService),
56+
rename(picoRenamingService, prepareRenameService = picoRenamePreparingService)
5657
};
5758

5859
@synopsis{This set of contributions runs slower but provides more detail.}
@@ -177,6 +178,15 @@ value picoExecutionService(removeDecl(start[Program] program, IdType toBeRemoved
177178
return ("result": true);
178179
}
179180
181+
loc picoRenamePreparingService(Focus _:[Id id, *_]) = id.src;
182+
183+
tuple[list[DocumentEdit], set[Message]] picoRenamingService(Focus focus, str newName) = <[changed(focus[0].src.top, [
184+
replace(id.src, newName)
185+
| cursor := focus[0]
186+
, /Id id := focus[-1]
187+
, id := cursor
188+
])], {}>;
189+
180190
@synopsis{The main function registers the Pico language with the IDE}
181191
@description{
182192
Register the Pico language and the contributions that supply the IDE with features.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import analysis::diff::edits::TextEdits;
4444
import IO;
4545
import ParseTree;
4646
import Message;
47+
import Exception;
4748

4849
@synopsis{Definition of a language server by its meta-data.}
4950
@description{
@@ -207,6 +208,9 @@ hover documentation, definition with uses, references to declarations, implement
207208
* The ((inlayHint)) service discovers plances to add "inlays" (little views embedded in the editor on the same line). Unlike ((lenses)) inlays do not offer command execution.
208209
* The ((execution)) service executes the commands registered by ((lenses)) and ((inlayHinter))s.
209210
* The ((actions)) service discovers places in the editor to add "code actions" (little hints in the margin next to where the action is relevant) and connects ((CodeAction))s to execute when the users selects the action from a menu.
211+
* The ((util::LanguageServer::rename)) service renames an identifier by collecting the edits required to rename all occurrences of that identifier. It might fail and report why in diagnostics.
212+
* The optional `prepareRename` service argument discovers places in the editor where a ((util::LanguageServer::rename)) is possible. If renameing the location is not supported, it should throw an exception.
213+
* The ((didRenameFiles)) service collects ((DocumentEdit))s corresponding to renamed files (e.g. to rename a class when the class file was renamed). The IDE applies the edits after moving the files. It might fail and report why in diagnostics.
210214

211215
Many services receive a ((Focus)) parameter. The focus lists the syntactical constructs under the current cursor, from the current
212216
leaf all the way up to the root of the tree. This list helps to create functionality that is syntax-directed, and always relevant to the
@@ -268,8 +272,14 @@ data LanguageService
268272
| references (set[loc] (Focus _focus) referencesService)
269273
| implementation(set[loc] (Focus _focus) implementationService)
270274
| codeAction (list[CodeAction] (Focus _focus) codeActionService)
275+
| rename (tuple[list[DocumentEdit], set[Message]] (Focus _focus, str newName) renameService
276+
, loc (Focus _focus) prepareRenameService = defaultPrepareRenameService)
277+
| didRenameFiles(tuple[list[DocumentEdit], set[Message]] (list[DocumentEdit] fileRenames) didRenameFilesService)
271278
;
272279

280+
loc defaultPrepareRenameService(Focus _:[Tree tr, *_]) = tr.src when tr.src?;
281+
default loc defaultPrepareRenameService(Focus focus) { throw IllegalArgument(focus, "Element under cursor does not have source location"); }
282+
273283
@deprecated{Backward compatible with ((parsing)).}
274284
@synopsis{Construct a `parsing` ((LanguageService))}
275285
LanguageService parser(Parser parser) = parsing(parser);

0 commit comments

Comments
 (0)