Skip to content

Commit

Permalink
Support for prepare rename default behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaeumer committed Sep 7, 2020
1 parent 77c3a10 commit f9c16a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions client/src/common/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,10 @@ class DocumentOnTypeFormattingFeature extends TextDocumentFeature<DocumentOnType
}
}

interface DefaultBehavior {
defaultBehavior: boolean;
}

class RenameFeature extends TextDocumentFeature<boolean | RenameOptions, RenameRegistrationOptions, RenameProvider> {

constructor(client: BaseLanguageClient) {
Expand All @@ -2165,6 +2169,7 @@ class RenameFeature extends TextDocumentFeature<boolean | RenameOptions, RenameR
let rename = ensure(ensure(capabilites, 'textDocument')!, 'rename')!;
rename.dynamicRegistration = true;
rename.prepareSupport = true;
rename.prepareSupportDefaultBehavior = true;
}

public initialize(capabilities: ServerCapabilities, documentSelector: DocumentSelector): void {
Expand Down Expand Up @@ -2211,6 +2216,10 @@ class RenameFeature extends TextDocumentFeature<boolean | RenameOptions, RenameR
return client.sendRequest(PrepareRenameRequest.type, params, token).then((result) => {
if (Range.is(result)) {
return client.protocol2CodeConverter.asRange(result);
} else if (this.isDefaultBehavior(result)) {
return result.defaultBehavior === true
? null
: Promise.reject(new Error(`The element can't be renamed.`));
} else if (result && Range.is(result.range)) {
return {
range: client.protocol2CodeConverter.asRange(result.range),
Expand All @@ -2234,6 +2243,11 @@ class RenameFeature extends TextDocumentFeature<boolean | RenameOptions, RenameR
};
return [Languages.registerRenameProvider(options.documentSelector!, provider), provider];
}

private isDefaultBehavior(value: any): value is DefaultBehavior {
const candidate: DefaultBehavior = value;
return candidate && Is.boolean(candidate.defaultBehavior);
}
}

class DocumentLinkFeature extends TextDocumentFeature<DocumentLinkOptions, DocumentLinkRegistrationOptions, DocumentLinkProvider> {
Expand Down
11 changes: 10 additions & 1 deletion protocol/src/common/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,13 @@ export interface RenameClientCapabilities {
* @since version 3.12.0
*/
prepareSupport?: boolean;

/**
* Client supports the default behavior result.
*
* @since version 3.16.0
*/
prepareSupportDefaultBehavior?: boolean;
}

/**
Expand Down Expand Up @@ -2651,10 +2658,12 @@ export interface PrepareRenameParams extends TextDocumentPositionParams, WorkDon

/**
* A request to test and perform the setup necessary for a rename.
*
* @since 3.16 - support for default behavior
*/
export namespace PrepareRenameRequest {
export const method: 'textDocument/prepareRename' = 'textDocument/prepareRename';
export const type = new ProtocolRequestType<PrepareRenameParams, Range | { range: Range, placeholder: string } | null, never, void, void>(method);
export const type = new ProtocolRequestType<PrepareRenameParams, Range | { range: Range, placeholder: string } | { defaultBehavior: boolean } | null, never, void, void>(method);
}

//---- Command Execution -------------------------------------------
Expand Down

0 comments on commit f9c16a9

Please sign in to comment.