UNPKG

coc-fuior

Version:

Language support for Fuior (narrative DSL)

219 lines 8.81 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.activateDiagnostics = exports.refreshDiagnostics = void 0; const vscode = __importStar(require("coc.nvim")); const debounce = require("lodash.debounce"); const path = __importStar(require("path")); const cp = __importStar(require("child_process")); const fs_1 = require("fs"); class DiagnosticHandler { constructor(doc, fuiorDiagnosics) { this.pending = false; this.requested = false; this.document = doc; this.fuiorDiagnostics = fuiorDiagnosics; this.debounced = debounce(() => { if (this.pending) { this.requested = true; return; } this.run(); }, 500); } findSubdir(pathComponents) { return __awaiter(this, void 0, void 0, function* () { let fsPath = vscode.Uri.parse(this.document.uri).fsPath; if (!fsPath) return null; while (true) { const dirname = path.dirname(fsPath); if (dirname === fsPath) break; fsPath = dirname; const exePath = path.join(dirname, ...pathComponents); try { yield new Promise((resolve, reject) => { fs_1.stat(exePath, (err, data) => { if (err) reject(err); resolve(data); }); }); return exePath; } catch (ex) { } } }); } getLinterPath() { return __awaiter(this, void 0, void 0, function* () { let osname = process.platform; let extension = ""; if (osname.startsWith("darwin")) { osname = "Darwin"; } else if (osname.startsWith("win")) { osname = "Windows"; extension = ".exe"; } else if (osname.startsWith("linux")) { osname = "Linux"; } return yield this.findSubdir([ "deploy", "config", "hooks", "fuior", "bin", osname, "fuior" + extension, ]); }); } getHeaderPath() { return __awaiter(this, void 0, void 0, function* () { const path = yield this.findSubdir(["config.fui"]); if (path === vscode.Uri.parse(this.document.uri).fsPath) return null; return path; }); } runLinter() { return __awaiter(this, void 0, void 0, function* () { if (this.lastUri !== this.document.uri.toString()) { this.lastUri = this.document.uri.toString(); this.exePath = yield this.getLinterPath(); this.headerPath = yield this.getHeaderPath(); } if (!this.exePath) return; const fuiorArgs = ["-", "--no-generate", "--error-ranges"]; const workspaceDir = vscode.Uri.parse(vscode.workspace.getWorkspaceFolder(this.document.uri).uri).fsPath; if (workspaceDir) { fuiorArgs.push("--import-root"); fuiorArgs.push(workspaceDir); } if (this.headerPath) { fuiorArgs.push("--header"); fuiorArgs.push(this.headerPath); } const linterOutput = yield new Promise((resolve, reject) => { const sp = cp.spawn(this.exePath, fuiorArgs, { stdio: ["pipe", "ignore", "pipe"], }); sp.on("error", reject); const chunks = []; sp.stderr.on("data", (data) => { chunks.push(data); }); try { sp.stdin.write(Buffer.from(this.document.getText(), "utf-8")); sp.stdin.end(); } catch (ex) { } sp.stderr.on("error", reject); sp.stderr.on("end", () => { resolve(Buffer.concat(chunks).toString("utf-8")); }); }); if (!this.pending) return; const diagnostics = []; linterOutput.split("\n").forEach((line) => { const match = line.match(/(WARNING|ERROR): stdin:([0-9]+):([0-9]+)\-([0-9]+):([0-9]+) (.*)/); if (!match) return; const errorType = match[1]; const startRow = parseInt(match[2]) - 1; const startColumn = parseInt(match[3]) - 1; const endRow = parseInt(match[4]) - 1; const endColumn = parseInt(match[5]) - 1; const message = match[6]; diagnostics.push(vscode.Diagnostic.create(vscode.Range.create(startRow, startColumn, endRow, endColumn), message, errorType === "ERROR" ? vscode.DiagnosticSeverity.Error : vscode.DiagnosticSeverity.Warning)); }); this.fuiorDiagnostics.set(this.document.uri, diagnostics); }); } run() { return __awaiter(this, void 0, void 0, function* () { this.pending = true; this.requested = false; try { yield this.runLinter(); } catch (ex) { console.error("Error running linter: ", ex); } this.pending = false; if (this.requested) { this.run(); } }); } refresh() { this.debounced(); } cancel() { this.requested = false; this.pending = false; this.debounced.cancel(); } } function refreshDiagnostics(doc, fuiorDiagnostics, handlers) { if (doc.languageId !== "fuior") return; let handler = handlers.get(doc); if (!handler) { handler = new DiagnosticHandler(doc, fuiorDiagnostics); handlers.set(doc, handler); } handler.refresh(); } exports.refreshDiagnostics = refreshDiagnostics; const activateDiagnostics = (context) => __awaiter(void 0, void 0, void 0, function* () { const fuiorDiagnostics = vscode.languages.createDiagnosticCollection("fuior"); context.subscriptions.push(fuiorDiagnostics); const handlers = new Map(); const currentDocument = yield vscode.workspace.document; if (currentDocument) { refreshDiagnostics(currentDocument.textDocument, fuiorDiagnostics, handlers); } context.subscriptions.push(vscode.workspace.onDidChangeTextDocument((e) => refreshDiagnostics(vscode.workspace.getDocument(e.bufnr).textDocument, fuiorDiagnostics, handlers))); context.subscriptions.push(vscode.workspace.onDidCloseTextDocument((doc) => { var _a; fuiorDiagnostics.delete(doc.uri); (_a = handlers.get(doc)) === null || _a === void 0 ? void 0 : _a.cancel(); })); }); exports.activateDiagnostics = activateDiagnostics; //# sourceMappingURL=diagnostics.js.map