UNPKG

xstate-tsserver

Version:

XState tsserver plugin for easier navigation in state machines

86 lines 3.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createParser = createParser; exports.getFileRootNode = getFileRootNode; exports.findCaptureNodeWithText = findCaptureNodeWithText; exports.findAllCaptureMatches = findAllCaptureMatches; exports.findFirstMatchingNode = findFirstMatchingNode; exports.findMatchingNode = findMatchingNode; exports.getAllCapturesOfMatch = getAllCapturesOfMatch; const tree_sitter_1 = __importDefault(require("tree-sitter")); const tree_sitter_typescript_1 = __importDefault(require("tree-sitter-typescript")); function createParser() { const parser = new tree_sitter_1.default(); parser.setLanguage(tree_sitter_typescript_1.default.typescript); return parser; } function getFileRootNode(sourceFile) { const parser = createParser(); const tree = parser.parse(sourceFile.getFullText()); return tree.rootNode; } function findCaptureNodeWithText(rootNode, queryString, captureName, captureText) { const parser = createParser(); const queryMatches = new tree_sitter_1.default.Query(parser.getLanguage(), queryString); const matches = queryMatches.matches(rootNode); for (const match of matches) { const captureNode = match.captures.find((cap) => cap.name === captureName && cap.node.text === captureText)?.node; if (captureNode) { return captureNode; } } } function findAllCaptureMatches(rootNode, queryString) { const parser = createParser(); const queryMatches = new tree_sitter_1.default.Query(parser.getLanguage(), queryString); const matches = queryMatches.matches(rootNode); const results = {}; for (const match of matches) { for (const capture of match.captures) { results[capture.name] = capture.node; } } return results; } function findFirstMatchingNode(rootNode, queryString, captureMatch) { const parser = createParser(); const queryMatches = new tree_sitter_1.default.Query(parser.getLanguage(), queryString); const matches = queryMatches.matches(rootNode); for (const match of matches) { const keyNode = match.captures.find((cap) => cap.name === captureMatch)?.node; if (keyNode) { return keyNode; } } return null; } function findMatchingNode(rootNode, position, queryString, captureMatch, returnCaptureMatch) { const parser = createParser(); const queryMatches = new tree_sitter_1.default.Query(parser.getLanguage(), queryString); const matches = queryMatches.matches(rootNode); for (const match of matches) { const keyNode = match.captures.find((cap) => cap.name === captureMatch)?.node; if (keyNode) { if (position >= keyNode.startIndex && position < keyNode.endIndex) { if (!returnCaptureMatch) return keyNode; const returnNode = match.captures.find((cap) => cap.name === returnCaptureMatch)?.node; if (returnNode) { return returnNode; } } } } return null; } function getAllCapturesOfMatch(match) { const results = {}; for (const capture of match.captures) { results[capture.name] = capture.node; } return results; } //# sourceMappingURL=treesitter.js.map