UNPKG

plaxtony

Version:

Static code analysis of SC2 Galaxy Script

107 lines 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RenameProvider = void 0; const provider_1 = require("./provider"); const lsp = require("vscode-languageserver"); const checker_1 = require("../compiler/checker"); const utils_1 = require("./utils"); const utils_2 = require("../compiler/utils"); function deepEqual(x, y) { const ok = Object.keys, tx = typeof x, ty = typeof y; return x && y && tx === 'object' && tx === ty ? (ok(x).length === ok(y).length && ok(x).every(key => deepEqual(x[key], y[key]))) : (x === y); } ; class RenameProvider extends provider_1.AbstractProvider { getTokenAt(params) { const sourceFile = this.store.documents.get(params.textDocument.uri); if (!sourceFile) return null; const position = utils_1.getPositionOfLineAndCharacter(sourceFile, params.position.line, params.position.character); const currentToken = utils_1.getAdjacentIdentfier(position, sourceFile); if (!currentToken) return null; const checker = new checker_1.TypeChecker(this.store); const symbol = checker.getSymbolAtLocation(currentToken); if (!symbol) return null; return { sourceFile, identifier: currentToken, symbol, }; } locationsToWorkspaceEdits(locations, newText) { const workspaceEdit = { changes: {}, }; for (const loc of locations) { if (typeof workspaceEdit.changes[loc.uri] === "undefined") { workspaceEdit.changes[loc.uri] = []; } workspaceEdit.changes[loc.uri].push({ range: loc.range, newText: newText, }); } return workspaceEdit; } prefetchLocations() { if (this.recentRequest && !this.recentRequest.locations) { this.recentRequest.locations = this.referencesProvider.onReferences({ textDocument: this.recentRequest.params.textDocument, position: this.recentRequest.params.position, context: { includeDeclaration: true }, }, true); } } onPrepareRename(params) { // const sourceFile = this.store.documents.get(params.textDocument.uri); const result = this.getTokenAt(params); if (!result) { return new lsp.ResponseError(lsp.ErrorCodes.RequestCancelled, 'Not an identifier'); } for (const decl of result.symbol.declarations) { const declSourceFile = utils_2.getSourceFileOfNode(decl); const sfMeta = this.store.documents.get(declSourceFile.fileName); if ((this.store.s2workspace && sfMeta && sfMeta.s2meta && sfMeta.s2meta.file.archive.isBuiltin) // (!this.store.s2workspace && !this.store.openDocuments.has(declSourceFile.fileName)) ) { return new lsp.ResponseError(lsp.ErrorCodes.RequestCancelled, `Cannot rename identifier from built-in dependency: ${sfMeta.s2meta.file.archive.name}`); } } this.recentRequest = { params: params, ...result, }; return { placeholder: result.symbol.escapedName, range: { start: utils_1.getLineAndCharacterOfPosition(utils_2.getSourceFileOfNode(result.identifier), result.identifier.pos), end: utils_1.getLineAndCharacterOfPosition(utils_2.getSourceFileOfNode(result.identifier), result.identifier.end), }, }; } onRenameRequest(params) { const sourceFile = this.store.documents.get(params.textDocument.uri); if (!sourceFile) return; if (!this.recentRequest || !deepEqual(this.recentRequest.params, { textDocument: params.textDocument, position: params.position }) || this.recentRequest.sourceFile !== sourceFile) { return; } if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(params.newName)) { return new lsp.ResponseError(lsp.ErrorCodes.RequestCancelled, 'Invalid name'); } if (this.recentRequest.symbol.parent && this.recentRequest.symbol.parent.members.has(params.newName)) { return new lsp.ResponseError(lsp.ErrorCodes.RequestCancelled, 'Name already in use'); } if (this.recentRequest.locations) { return this.locationsToWorkspaceEdits(this.recentRequest.locations, params.newName); } } } exports.RenameProvider = RenameProvider; //# sourceMappingURL=rename.js.map