Skip to content

Commit

Permalink
(feat) basic rename (#172)
Browse files Browse the repository at this point in the history
* (feat) basic rename

#110

* (feat) handle prop rename of component A inside component B

* (feat) support for rename of prop of A in A

* (fix) linting

* (fix) fix range conversion, fix word retrival, tests

* (fix) don't rename everything

* (feat) add prepareRename support

To show a message early that rename is not allowed in certain locations

* (fix) use modified mapRangeToOriginal

* (fix) don't rename in strings

* (fix) don't allow rename of import path

We cannot handle it at the moment

* (fix) support prop rename without types

For that svelte2tsx needs to write out the props as {prop: prop}

* (fix) prevent renames in error state

* (fix) use ts service for getting variable at position

* (fix) convert bind so that source mapping is correct
  • Loading branch information
dummdidumm authored Jun 18, 2020
1 parent 2bb3cdd commit f483271
Show file tree
Hide file tree
Showing 27 changed files with 748 additions and 22 deletions.
7 changes: 7 additions & 0 deletions packages/language-server/src/lib/documents/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,10 @@ export function isInTag(position: Position, tagInfo: TagInformation | null): boo
export function getTextInRange(range: Range, text: string) {
return text.substring(offsetAt(range.start, text), offsetAt(range.end, text));
}

export function getLineAtPosition(position: Position, text: string) {
return text.substring(
offsetAt({ line: position.line, character: 0 }, text),
offsetAt({ line: position.line, character: Number.MAX_VALUE }, text),
);
}
33 changes: 33 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,39 @@ export class PluginHost implements LSProvider, OnWatchFileChanges {
);
}

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

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

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
12 changes: 11 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,15 @@ export interface UpdateImportsProvider {
updateImports(fileRename: FileRename): Resolvable<WorkspaceEdit | null>;
}

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

export interface OnWatchFileChanges {
onWatchFileChanges(fileName: string, changeType: FileChangeType): void;
}
Expand All @@ -109,6 +118,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 @@ -32,6 +32,7 @@ import {
FileRename,
HoverProvider,
OnWatchFileChanges,
RenameProvider,
UpdateImportsProvider,
} from '../interfaces';
import { SnapshotFragment } from './DocumentSnapshot';
Expand All @@ -49,6 +50,7 @@ import {
getScriptKindFromFileName,
symbolKindFromString,
} from './utils';
import { RenameProviderImpl } from './features/RenameProvider';

export class TypeScriptPlugin
implements
Expand All @@ -58,6 +60,7 @@ export class TypeScriptPlugin
DefinitionsProvider,
CodeActionsProvider,
UpdateImportsProvider,
RenameProvider,
OnWatchFileChanges,
CompletionsProvider<CompletionEntryWithIdentifer> {
private readonly configManager: LSConfigManager;
Expand All @@ -66,6 +69,7 @@ export class TypeScriptPlugin
private readonly codeActionsProvider: CodeActionsProviderImpl;
private readonly updateImportsProvider: UpdateImportsProviderImpl;
private readonly diagnosticsProvider: DiagnosticsProviderImpl;
private readonly renameProvider: RenameProviderImpl;

constructor(
docManager: DocumentManager,
Expand All @@ -78,6 +82,7 @@ export class TypeScriptPlugin
this.codeActionsProvider = new CodeActionsProviderImpl(this.lsAndTsDocResolver);
this.updateImportsProvider = new UpdateImportsProviderImpl(this.lsAndTsDocResolver);
this.diagnosticsProvider = new DiagnosticsProviderImpl(this.lsAndTsDocResolver);
this.renameProvider = new RenameProviderImpl(this.lsAndTsDocResolver);
}

async getDiagnostics(document: Document): Promise<Diagnostic[]> {
Expand Down Expand Up @@ -227,6 +232,26 @@ export class TypeScriptPlugin
);
}

async prepareRename(document: Document, position: Position): Promise<Range | null> {
if (!this.featureEnabled('rename')) {
return null;
}

return this.renameProvider.prepareRename(document, position);
}

async rename(
document: Document,
position: Position,
newName: string,
): Promise<WorkspaceEdit | null> {
if (!this.featureEnabled('rename')) {
return null;
}

return this.renameProvider.rename(document, position, newName);
}

async getCodeActions(
document: Document,
range: Range,
Expand Down
Loading

0 comments on commit f483271

Please sign in to comment.