UNPKG

fish-lsp

Version:

LSP implementation for fish/fish-shell

151 lines (150 loc) 5.68 kB
"use strict"; // https://github.com/typescript-language-server/typescript-language-server/blob/5a39c1f801ab0cad725a2b8711c0e0d46606a08b/src/utils/typeConverters.ts#L12 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.Location = exports.Position = exports.Range = void 0; const LSP = __importStar(require("vscode-languageserver")); var Range; (function (Range) { Range.create = (start, end) => LSP.Range.create(start, end); Range.is = (value) => LSP.Range.is(value); Range.fromTextSpan = (span) => Range.fromLocations(span.start, span.end); Range.toTextSpan = (range) => ({ start: Position.toLocation(range.start), end: Position.toLocation(range.end), }); Range.fromLocations = (start, end) => LSP.Range.create(Math.max(0, start.line - 1), Math.max(start.offset - 1, 0), Math.max(0, end.line - 1), Math.max(0, end.offset - 1)); Range.toFileRangeRequestArgs = (file, range) => ({ file, startLine: range.start.line + 1, startOffset: range.start.character + 1, endLine: range.end.line + 1, endOffset: range.end.character + 1, }); Range.toFormattingRequestArgs = (file, range) => ({ file, line: range.start.line + 1, offset: range.start.character + 1, endLine: range.end.line + 1, endOffset: range.end.character + 1, }); function intersection(one, other) { const start = Position.Max(other.start, one.start); const end = Position.Min(other.end, one.end); if (Position.isAfter(start, end)) { // this happens when there is no overlap: // |-----| // |----| return undefined; } return LSP.Range.create(start, end); } Range.intersection = intersection; })(Range || (exports.Range = Range = {})); var Position; (function (Position) { Position.create = (line, character) => LSP.Position.create(line, character); Position.is = (value) => LSP.Position.is(value); Position.fromLocation = (fishlocation) => { // Clamping on the low side to 0 since Typescript returns 0, 0 when creating new file // even though position is supposed to be 1-based. return { line: Math.max(fishlocation.line - 1, 0), character: Math.max(fishlocation.offset - 1, 0), }; }; Position.toLocation = (position) => ({ line: position.line + 1, offset: position.character + 1, }); Position.toFileLocationRequestArgs = (file, position) => ({ file, line: position.line + 1, offset: position.character + 1, }); function Min(...positions) { if (!positions.length) { return undefined; } let result = positions.pop(); for (const p of positions) { if (isBefore(p, result)) { result = p; } } return result; } Position.Min = Min; function isBefore(one, other) { if (one.line < other.line) { return true; } if (other.line < one.line) { return false; } return one.character < other.character; } Position.isBefore = isBefore; function Max(...positions) { if (!positions.length) { return undefined; } let result = positions.pop(); for (const p of positions) { if (isAfter(p, result)) { result = p; } } return result; } Position.Max = Max; function isAfter(one, other) { return !isBeforeOrEqual(one, other); } Position.isAfter = isAfter; function isBeforeOrEqual(one, other) { if (one.line < other.line) { return true; } if (other.line < one.line) { return false; } return one.character <= other.character; } Position.isBeforeOrEqual = isBeforeOrEqual; function fromSyntaxNode(node) { return { start: Position.create(node.startPosition.row, node.endPosition.column), end: Position.create(node.endPosition.row, node.endPosition.column), }; } Position.fromSyntaxNode = fromSyntaxNode; })(Position || (exports.Position = Position = {})); var Location; (function (Location) { Location.create = (uri, range) => LSP.Location.create(uri, range); Location.is = (value) => LSP.Location.is(value); Location.fromTextSpan = (resource, fishTextSpan) => LSP.Location.create(resource, Range.fromTextSpan(fishTextSpan)); })(Location || (exports.Location = Location = {}));