UNPKG

perlnavigator-server

Version:

Perl language server

121 lines 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatRange = exports.formatDoc = void 0; const node_1 = require("vscode-languageserver/node"); const utils_1 = require("./utils"); const path_1 = require("path"); const vscode_uri_1 = require("vscode-uri"); const assets_1 = require("./assets"); const progress_1 = require("./progress"); async function formatDoc(params, txtDoc, settings, workspaceFolders, connection) { return await maybeReturnEdits(node_1.Range.create(node_1.Position.create(0, 0), node_1.Position.create(txtDoc.lineCount, 0)), txtDoc, settings, workspaceFolders, connection); } exports.formatDoc = formatDoc; async function formatRange(params, txtDoc, settings, workspaceFolders, connection) { const offset = params.range.end.character > 0 ? 1 : 0; return await maybeReturnEdits(node_1.Range.create(node_1.Position.create(params.range.start.line, 0), node_1.Position.create(params.range.end.line + offset, 0)), txtDoc, settings, workspaceFolders, connection); } exports.formatRange = formatRange; async function maybeReturnEdits(range, txtDoc, settings, workspaceFolders, connection) { const text = txtDoc.getText(range); if (!text) { return; } const progressToken = await (0, progress_1.startProgress)(connection, "Formatting doc", settings); let newSource = ""; const fixedImports = await perlimports(txtDoc, text, settings); if (fixedImports) { newSource = fixedImports; } const tidedSource = await perltidy(fixedImports || text, settings, workspaceFolders); if (tidedSource) { newSource = tidedSource; } (0, progress_1.endProgress)(connection, progressToken); if (!newSource) { // If we failed on both tidy and imports return; } const edits = { range: range, newText: newSource, }; return [edits]; } async function perlimports(doc, code, settings) { if (!settings.perlimportsTidyEnabled) return; const importsPath = (0, path_1.join)(await (0, assets_1.getPerlAssetsPath)(), "perlimportsWrapper.pl"); let cliParams = [importsPath].concat((0, utils_1.getPerlimportsProfile)(settings)); cliParams = cliParams.concat(["--filename", vscode_uri_1.default.parse(doc.uri).fsPath]); (0, utils_1.nLog)("Now starting perlimports with: " + cliParams.join(" "), settings); try { const process = (0, utils_1.async_execFile)(settings.perlPath, settings.perlParams.concat(cliParams), { timeout: 25000, maxBuffer: 20 * 1024 * 1024 }); process?.child?.stdin?.on("error", (error) => { (0, utils_1.nLog)("perlImports Error Caught: ", settings); (0, utils_1.nLog)(error, settings); }); process?.child?.stdin?.write(code); process?.child?.stdin?.end(); const out = await process; return out.stdout; } catch (error) { (0, utils_1.nLog)("Attempted to run perlimports tidy " + error.stdout, settings); return; } } async function perltidy(code, settings, workspaceFolders) { if (!settings.perltidyEnabled) return; const tidy_path = (0, path_1.join)(await (0, assets_1.getPerlAssetsPath)(), "tidyWrapper.pl"); let tidyParams = [tidy_path].concat(getTidyProfile(workspaceFolders, settings)); (0, utils_1.nLog)("Now starting perltidy with: " + tidyParams.join(" "), settings); let output; try { const process = (0, utils_1.async_execFile)(settings.perlPath, settings.perlParams.concat(tidyParams), { timeout: 25000, maxBuffer: 20 * 1024 * 1024 }); process?.child?.stdin?.on("error", (error) => { (0, utils_1.nLog)("PerlTidy Error Caught: ", settings); (0, utils_1.nLog)(error, settings); }); process?.child?.stdin?.write(code); process?.child?.stdin?.end(); const out = await process; output = out.stdout; } catch (error) { (0, utils_1.nLog)("Perltidy failed with unknown error", settings); (0, utils_1.nLog)(error, settings); return; } let pieces = output.split("ee4ffa34-a14f-4609-b2e4-bf23565f3262"); if (pieces.length > 1) { return pieces[1]; } else { return; } } function getTidyProfile(workspaceFolders, settings) { let profileCmd = []; if (settings.perltidyProfile) { let profile = settings.perltidyProfile; if (profile.indexOf("$workspaceFolder") != -1) { if (workspaceFolders) { // TODO: Fix this. Only uses the first workspace folder const workspaceUri = vscode_uri_1.default.parse(workspaceFolders[0].uri).fsPath; profileCmd.push("--profile"); profileCmd.push(profile.replaceAll("$workspaceFolder", workspaceUri)); } else { (0, utils_1.nLog)("You specified $workspaceFolder in your perltidy path, but didn't include any workspace folders. Ignoring profile.", settings); } } else { profileCmd.push("--profile"); profileCmd.push(profile); } } return profileCmd; } //# sourceMappingURL=formatting.js.map