From c807dc4cfaa21c8909ec30a830a0cad733a2f690 Mon Sep 17 00:00:00 2001 From: ivojawer Date: Sat, 16 Dec 2023 20:46:41 -0300 Subject: [PATCH] vscode is bugged, return range instead of defaultBehavior --- package-lock.json | 11 +++++++++++ server/src/functionalities/rename.ts | 27 ++++++++++++++------------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4aed3250..314098ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,7 @@ }, "../wollok-ts": { "version": "4.0.6", + "extraneous": true, "license": "MIT", "dependencies": { "@types/parsimmon": "^1.10.8", @@ -1023,6 +1024,11 @@ "integrity": "sha512-93+VvleD3mXwlLI/xASjw0FzKcwzl3OdTCzm1LaRfqgS21gfFtK3zDXM5Op9TeeMsJVOaJ2VRDpT9q4Y3d0AvA==", "dev": true }, + "node_modules/@types/parsimmon": { + "version": "1.10.9", + "resolved": "https://registry.npmjs.org/@types/parsimmon/-/parsimmon-1.10.9.tgz", + "integrity": "sha512-O2M2x1w+m7gWLen8i5DOy6tWRnbRcsW6Pke3j3HAsJUrPb4g0MgjksIUm2aqUtCYxy7Qjr3CzjjwQBzhiGn46A==" + }, "node_modules/@types/semver": { "version": "7.3.13", "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.3.13.tgz", @@ -6794,6 +6800,11 @@ "integrity": "sha512-93+VvleD3mXwlLI/xASjw0FzKcwzl3OdTCzm1LaRfqgS21gfFtK3zDXM5Op9TeeMsJVOaJ2VRDpT9q4Y3d0AvA==", "dev": true }, + "@types/parsimmon": { + "version": "1.10.9", + "resolved": "https://registry.npmjs.org/@types/parsimmon/-/parsimmon-1.10.9.tgz", + "integrity": "sha512-O2M2x1w+m7gWLen8i5DOy6tWRnbRcsW6Pke3j3HAsJUrPb4g0MgjksIUm2aqUtCYxy7Qjr3CzjjwQBzhiGn46A==" + }, "@types/semver": { "version": "7.3.13", "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.3.13.tgz", diff --git a/server/src/functionalities/rename.ts b/server/src/functionalities/rename.ts index 83dedffd..2124f3d2 100644 --- a/server/src/functionalities/rename.ts +++ b/server/src/functionalities/rename.ts @@ -6,33 +6,34 @@ import { cursorNode, fileNameToURI, toVSCRange } from '../utils/text-documents' import { referenceOf } from '../utils/vm/wollok' export const rename = (documents: TextDocuments) => (environment: Environment) => (params: RenameParams): WorkspaceEdit | null => { - const renamedNode = cursorNode(environment, params.position, params.textDocument) - + // cast cursor node as it's already validated in prepareRename request + const renamedNode = cursorNode(environment, params.position, params.textDocument) as Renamable | Reference | undefined if(!renamedNode) throw new Error('No node found at position') - if(renamedNode.is(Reference) && renamedNode.target && isRenamable(renamedNode.target)){ - return { changes: groupByURI(renameNode(renamedNode.target, params.newName, environment, documents)) } - } - if(isRenamable(renamedNode)) { - return { changes: groupByURI(renameNode(renamedNode, params.newName, environment, documents)) } + return { + changes: renamedNode.is(Reference) ? + groupByURI(renameNode(renamedNode.target!, params.newName, environment, documents)) : + groupByURI(renameNode(renamedNode, params.newName, environment, documents)), } - - return null } export const requestIsRenamable = (environment: Environment) => (params: RenameParams): any => { const renamedNode = cursorNode(environment, params.position, params.textDocument) if(!renamedNode) return null - if( renamedNode.is(Reference) && renamedNode.target && isRenamable(renamedNode.target) || isRenamable(renamedNode)){ - return { defaultBehavior: true } + if( renamedNode.is(Reference) && renamedNode.target && isRenamable(renamedNode.target) || isRenamable(renamedNode)) { + // ToDo: switch back to defaultBehavior when https://github.com/microsoft/vscode/issues/198423 is released + return { + range: toVSCRange(renamedNode.sourceMap!), + placeholder: renamedNode.name, + } } return null } type Renamable = Field | Parameter | Variable -function isRenamable(aNode: T): aNode is T & Renamable { - return aNode.is(Field) || aNode.is(Parameter) || aNode.is(Variable) +function isRenamable(aNode: Node): aNode is Renamable { + return aNode.is(Field) && !aNode.isProperty || aNode.is(Parameter) || aNode.is(Variable) } function renameNode(node: Renamable, newName: string, environment: Environment, documents: TextDocuments): {uri: string, edit: TextEdit}[]{