Skip to content

Commit

Permalink
vscode is bugged, return range instead of defaultBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ivojawer committed Dec 16, 2023
1 parent 5143912 commit c807dc4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions server/src/functionalities/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@ import { cursorNode, fileNameToURI, toVSCRange } from '../utils/text-documents'
import { referenceOf } from '../utils/vm/wollok'

export const rename = (documents: TextDocuments<TextDocument>) => (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<Renamable> | 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<T extends Node>(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<TextDocument>): {uri: string, edit: TextEdit}[]{
Expand Down

0 comments on commit c807dc4

Please sign in to comment.