-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: renaming of declarations that are imported under an alias (#825)
Closes #635 ### Summary of Changes Declarations that are imported under an alias are now correctly ignored during renaming. The renaming behavior is always the same, no matter whether it is triggered on a declaration or any reference. An alias can only be changed manually.
- Loading branch information
1 parent
a74b8e0
commit 9f7363d
Showing
5 changed files
with
481 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
packages/safe-ds-lang/src/language/lsp/safe-ds-rename-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { | ||
AstNode, | ||
AstNodeLocator, | ||
DefaultRenameProvider, | ||
findDeclarationNodeAtOffset, | ||
getContainerOfType, | ||
LangiumDocument, | ||
LangiumDocuments, | ||
ReferenceDescription, | ||
Stream, | ||
URI, | ||
} from 'langium'; | ||
import { Position, RenameParams, TextEdit, WorkspaceEdit } from 'vscode-languageserver'; | ||
import { SafeDsServices } from '../safe-ds-module.js'; | ||
import { isSdsImportedDeclaration, isSdsModule } from '../generated/ast.js'; | ||
import { getImportedDeclarations } from '../helpers/nodeProperties.js'; | ||
|
||
export class SafeDsRenameProvider extends DefaultRenameProvider { | ||
private readonly astNodeLocator: AstNodeLocator; | ||
private readonly langiumDocuments: LangiumDocuments; | ||
|
||
constructor(services: SafeDsServices) { | ||
super(services); | ||
|
||
this.astNodeLocator = services.workspace.AstNodeLocator; | ||
this.langiumDocuments = services.shared.workspace.LangiumDocuments; | ||
} | ||
|
||
override async rename(document: LangiumDocument, params: RenameParams): Promise<WorkspaceEdit | undefined> { | ||
const references = this.findReferencesToRename(document, params.position); | ||
return this.createWorkspaceEdit(references, params.newName); | ||
} | ||
|
||
private findReferencesToRename( | ||
document: LangiumDocument, | ||
position: Position, | ||
): Stream<ReferenceDescription> | undefined { | ||
const rootNode = document.parseResult.value.$cstNode; | ||
if (!rootNode) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
const offset = document.textDocument.offsetAt(position); | ||
const leafNode = findDeclarationNodeAtOffset(rootNode, offset, this.grammarConfig.nameRegexp); | ||
if (!leafNode) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
const targetNode = this.references.findDeclaration(leafNode); | ||
if (!targetNode) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
const options = { onlyLocal: false, includeDeclaration: true }; | ||
return this.references.findReferences(targetNode, options).filter((ref) => this.mustBeRenamed(ref)); | ||
} | ||
|
||
private mustBeRenamed(ref: ReferenceDescription): boolean { | ||
// References in the same file must always be renamed | ||
if (ref.local) { | ||
return true; | ||
} | ||
|
||
// References in imported declaration nodes must always be renamed | ||
const sourceNode = this.getAstNode(ref.sourceUri, ref.sourcePath); | ||
if (isSdsImportedDeclaration(sourceNode)) { | ||
return true; | ||
} | ||
|
||
// Other references must be renamed unless they are imported under an alias | ||
const sourceModule = getContainerOfType(sourceNode, isSdsModule); | ||
if (!sourceModule) { | ||
/* c8 ignore next 2 */ | ||
return false; | ||
} | ||
|
||
const referenceText = this.getReferenceText(ref); | ||
if (!referenceText) { | ||
/* c8 ignore next 2 */ | ||
return false; | ||
} | ||
|
||
const targetNode = this.getAstNode(ref.targetUri, ref.targetPath); | ||
return !getImportedDeclarations(sourceModule).some((imp) => { | ||
return imp.declaration?.ref === targetNode && imp.alias?.alias === referenceText; | ||
}); | ||
} | ||
|
||
private getAstNode(uri: URI, path: string): AstNode | undefined { | ||
if (!this.langiumDocuments.hasDocument(uri)) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
const document = this.langiumDocuments.getOrCreateDocument(uri); | ||
return this.astNodeLocator.getAstNode(document.parseResult.value, path); | ||
} | ||
|
||
private getReferenceText(ref: ReferenceDescription): string | undefined { | ||
if (!this.langiumDocuments.hasDocument(ref.sourceUri)) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
const document = this.langiumDocuments.getOrCreateDocument(ref.sourceUri); | ||
return document.textDocument.getText(ref.segment.range); | ||
} | ||
|
||
private createWorkspaceEdit( | ||
references: Stream<ReferenceDescription> | undefined, | ||
newName: string, | ||
): WorkspaceEdit | undefined { | ||
if (!references) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
const changes: Record<string, TextEdit[]> = {}; | ||
|
||
references.forEach((ref) => { | ||
const change = TextEdit.replace(ref.segment.range, newName); | ||
const uri = ref.sourceUri.toString(); | ||
if (!changes[uri]) { | ||
changes[uri] = []; | ||
} | ||
|
||
changes[uri]!.push(change); | ||
}); | ||
|
||
return { changes }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.