fish-lsp
Version:
LSP implementation for fish/fish-shell
118 lines (117 loc) • 4.07 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPossible = isPossible;
exports.gatherVariableSiblings = gatherVariableSiblings;
exports.isSetDefinitionNode = isSetDefinitionNode;
exports.isReadDefinitionNode = isReadDefinitionNode;
exports.isFunctionArgumentDefinitionNode = isFunctionArgumentDefinitionNode;
exports.isForLoopDefinitionNode = isForLoopDefinitionNode;
const definition_scope_1 = require("./definition-scope");
const NodeTypes = __importStar(require("./node-types"));
function filterWordNodes(nodes) {
return nodes.filter(n => n.type === 'word');
}
function _setHasQuery(nodes) {
const options = filterWordNodes(nodes).filter(NodeTypes.isOption);
const queryFlag = new definition_scope_1.VariableDefinitionFlag('q', 'query');
for (const option of options) {
if (queryFlag.isMatch(option)) {
return true;
}
}
return false;
}
const shouldStop = (node) => {
return (NodeTypes.isCommand(node) ||
NodeTypes.isComment(node) ||
NodeTypes.isShebang(node) ||
NodeTypes.isSemicolon(node) ||
NodeTypes.isNewline(node));
};
function isPossible(node) {
return (node.type === 'variable_name' ||
node.type === 'word');
}
function gatherVariableSiblings(node) {
const siblings = [];
let next = node.nextSibling;
while (next && !shouldStop(next)) {
siblings.push(next);
next = next.nextSibling;
}
return siblings;
}
function isSetDefinitionNode(nodes, match) {
//if (setHasQuery(nodes)) return false;
for (const node of nodes) {
if (NodeTypes.isOption(node)) {
continue;
}
if (node.equals(match)) {
return true;
}
else {
return false;
}
}
return false;
}
function isReadDefinitionNode(siblings, match) {
const readVariables = [];
while (siblings.length > 0) {
const current = siblings.pop();
if (!current) {
break;
}
if (NodeTypes.isOption(current) || NodeTypes.isString(current)) {
break;
}
readVariables.push(current);
}
return readVariables.some(n => n.equals(match));
}
function isFunctionArgumentDefinitionNode(siblings, match) {
const argFlag = new definition_scope_1.VariableDefinitionFlag('a', 'argument-names');
const args = [];
for (let i = 0; i < siblings.length; i++) {
const child = siblings[i];
if (child && argFlag.isMatch(child)) {
let varName = child.nextSibling;
while (varName !== null && varName.type === 'word' && !varName.text.startsWith('-')) {
args.push(varName);
varName = varName.nextSibling;
}
}
}
return args.some(n => n.equals(match));
}
function isForLoopDefinitionNode(siblings, match) {
const first = siblings[0];
if (!first) {
return false;
}
return first.type === 'variable_name' && first.equals(match) || false;
}