Skip to content

Commit

Permalink
(feat) basic rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Holthausen committed Jun 6, 2020
1 parent 58c1c59 commit 675d4e1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/language-server/src/plugins/PluginHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,23 @@ export class PluginHost implements LSProvider, OnWatchFileChanges {
);
}

async rename(
textDocument: TextDocumentIdentifier,
position: Position,
newName: string,
): Promise<WorkspaceEdit | null> {
const document = this.getDocument(textDocument.uri);
if (!document) {
throw new Error('Cannot call methods on an unopened document');
}

return await this.execute<any>(
'rename',
[document, position, newName],
ExecuteMode.FirstNonNull,
);
}

onWatchFileChanges(fileName: string, changeType: FileChangeType): void {
for (const support of this.plugins) {
support.onWatchFileChanges?.(fileName, changeType);
Expand Down
11 changes: 10 additions & 1 deletion packages/language-server/src/plugins/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ export interface UpdateImportsProvider {
updateImports(fileRename: FileRename): Resolvable<WorkspaceEdit | null>;
}

export interface RenameProvider {
rename(
document: Document,
position: Position,
newName: string,
): Resolvable<WorkspaceEdit | null>;
}

export interface OnWatchFileChanges {
onWatchFileChanges(fileName: string, changeType: FileChangeType): void;
}
Expand All @@ -109,6 +117,7 @@ export type LSProvider = DiagnosticsProvider &
DocumentSymbolsProvider &
DefinitionsProvider &
UpdateImportsProvider &
CodeActionsProvider;
CodeActionsProvider &
RenameProvider;

export type Plugin = Partial<LSProvider & OnWatchFileChanges>;
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Document,
DocumentManager,
mapHoverToParent,
mapRangeToOriginal,
mapSymbolInformationToOriginal,
} from '../../lib/documents';
import { LSConfigManager, LSTypescriptConfig } from '../../ls-config';
Expand All @@ -32,6 +33,7 @@ import {
FileRename,
HoverProvider,
OnWatchFileChanges,
RenameProvider,
UpdateImportsProvider,
} from '../interfaces';
import { DocumentSnapshot, SnapshotFragment } from './DocumentSnapshot';
Expand All @@ -58,6 +60,7 @@ export class TypeScriptPlugin
DefinitionsProvider,
CodeActionsProvider,
UpdateImportsProvider,
RenameProvider,
OnWatchFileChanges,
CompletionsProvider<CompletionEntryWithIdentifer> {
private readonly configManager: LSConfigManager;
Expand Down Expand Up @@ -227,6 +230,55 @@ export class TypeScriptPlugin
);
}

async rename(
document: Document,
position: Position,
newName: string,
): Promise<WorkspaceEdit | null> {
const { lang, tsDoc } = this.getLSAndTSDoc(document);
const fragment = await tsDoc.getFragment();

const renameLocations = lang.findRenameLocations(
tsDoc.filePath,
fragment.offsetAt(fragment.getGeneratedPosition(position)),
true,
false,
);
if (!renameLocations) {
return null;
}

const docs = new Map<string, SnapshotFragment>([[tsDoc.filePath, fragment]]);
const convertedRenameLocations = await Promise.all(
renameLocations.map(async (loc) => {
let doc = docs.get(loc.fileName);
if (!doc) {
doc = await this.getSnapshot(loc.fileName).getFragment();
docs.set(loc.fileName, doc);
}

return {
...loc,
range: mapRangeToOriginal(doc, convertRange(doc, loc.textSpan)),
};
}),
);

return convertedRenameLocations
.filter((loc) => loc.range.start.line >= 0 && loc.range.end.line >= 0)
.reduce(
(acc, loc) => {
const uri = pathToUrl(loc.fileName);
if (!acc.changes[uri]) {
acc.changes[uri] = [];
}
acc.changes[uri].push({ newText: newName, range: loc.range });
return acc;
},
<Required<Pick<WorkspaceEdit, 'changes'>>>{ changes: {} },
);
}

async getCodeActions(
document: Document,
range: Range,
Expand Down
5 changes: 5 additions & 0 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ export function startServer(options?: LSOptions) {
],
}
: true,
renameProvider: true,
},
};
});

connection.onRenameRequest((req) =>
pluginHost.rename(req.textDocument, req.position, req.newName),
);

connection.onDidChangeConfiguration(({ settings }) => {
pluginHost.updateConfig(settings.svelte?.plugin);
});
Expand Down

0 comments on commit 675d4e1

Please sign in to comment.