diff options
author | bscan <10503608+bscan@users.noreply.github.com> | 2024-04-05 21:06:41 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-05 21:06:41 -0400 |
commit | ddc803bc72bd7e996519899167718be758205f83 (patch) | |
tree | 1211c6c5c517f3f16385283e3bfdcff6ba36db27 | |
parent | 3c58ec27222fcde34874bfac7ba3c5fa77188020 (diff) | |
parent | d439d93c34778a15ea1255da469f937363006a87 (diff) | |
download | PerlNavigator-ddc803bc72bd7e996519899167718be758205f83.zip |
Merge pull request #119 from nugged/env_vars_and_tilda_in_paths
Two commits (can be cherrypicked): Custom Env vars for Perl in settings, and convert tilda to home in paths
-rw-r--r-- | README.md | 13 | ||||
-rw-r--r-- | package.json | 12 | ||||
-rw-r--r-- | server/src/diagnostics.ts | 24 | ||||
-rw-r--r-- | server/src/server.ts | 36 | ||||
-rw-r--r-- | server/src/types.ts | 2 |
5 files changed, 82 insertions, 5 deletions
@@ -87,6 +87,19 @@ Sublime Text requires the following minimum settings under LSP settings (modify "command": ["node", "C:\\temp\\PerlNavigator\\server\\out\\server.js","--stdio"], "selector": "source.perl", }, + "settings": { + // "perlnavigator.perltidyProfile": "~/.perltidyrc", + // "perlnavigator.perlcriticProfile": "~/.perlcriticrc", + // "perlnavigator.perlEnvAdd": false, // default: true + // "perlnavigator.perlEnv": { + // "KOHA_CONF": "/home/user/git/KohaCommunity/t/data/koha-conf.xml", + // }, + // "perlnavigator.perlPath": "~/perl5/perlbrew/perls/perl-5.38.2/bin", + // "perlnavigator.perlcriticSeverity": 1, + // "perlnavigator.includePaths": [ "~/git/KohaCommunity", "~/git/KohaCommunity/lib" ], + // "perlnavigator.perlcriticEnabled": true, + // "perlnavigator.enableWarnings": true, + }, } } ``` diff --git a/package.json b/package.json index 58b4946..7429b64 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,18 @@ "default": [], "description": "Pass miscellaneous command line arguments to pass to the perl executable" }, + "perlnavigator.perlEnv": { + "scope": "resource", + "type": "object", + "default": {}, + "description": "Pass environment variables to the perl executable. Skipped if undefined." + }, + "perlnavigator.perlEnvAdd": { + "scope": "resource", + "type": "boolean", + "default": true, + "description": "Add environment variables to current environment, or totally replace (perlEnv related)." + }, "perlnavigator.enableWarnings": { "scope": "resource", "type": "boolean", diff --git a/server/src/diagnostics.ts b/server/src/diagnostics.ts index 64eec17..94618b9 100644 --- a/server/src/diagnostics.ts +++ b/server/src/diagnostics.ts @@ -19,6 +19,8 @@ export async function perlcompile(textDocument: TextDocument, workspaceFolders: return { diags: [], perlDoc: parsedDoc }; } let perlParams: string[] = [...settings.perlParams, "-c"]; + let perlEnv = settings.perlEnv; + let perlEnvAdd = settings.perlEnvAdd; const filePath = Uri.parse(textDocument.uri).fsPath; if (settings.enableWarnings) perlParams = perlParams.concat(["-Mwarnings", "-M-warnings=redefine"]); // Force enable some warnings. @@ -33,14 +35,26 @@ export async function perlcompile(textDocument: TextDocument, workspaceFolders: const diagnostics: Diagnostic[] = []; const code = getAdjustedPerlCode(textDocument, filePath); try { - const process = async_execFile(settings.perlPath, perlParams, { timeout: 10000, maxBuffer: 20 * 1024 * 1024 }); - process?.child?.stdin?.on("error", (error: any) => { + let options: { + timeout: number; + maxBuffer: number; + env?: { [key: string]: string | undefined }; + } = { timeout: 10000, maxBuffer: 20 * 1024 * 1024 }; + if (perlEnv) { + if (perlEnvAdd) { + options.env = { ...process.env, ...perlEnv }; + } else { + options.env = perlEnv; + } + } + const perlProcess = async_execFile(settings.perlPath, perlParams, options); + perlProcess?.child?.stdin?.on("error", (error: any) => { nLog("Perl Compilation Error Caught: ", settings); nLog(error, settings); }); - process?.child?.stdin?.write(code); - process?.child?.stdin?.end(); - const out = await process; + perlProcess?.child?.stdin?.write(code); + perlProcess?.child?.stdin?.end(); + const out = await perlProcess; output = out.stderr; stdout = out.stdout; diff --git a/server/src/server.ts b/server/src/server.ts index 4790ed0..7c5f873 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -17,6 +17,7 @@ import { TextEdit, } from "vscode-languageserver/node"; import { basename } from "path"; +import { homedir } from "os"; import { TextDocument } from "vscode-languageserver-textdocument"; import { PublishDiagnosticsParams } from "vscode-languageserver-protocol"; @@ -115,6 +116,8 @@ const defaultSettings: NavigatorSettings = { perlimportsTidyEnabled: false, perltidyEnabled: true, perlCompileEnabled: true, + perlEnv: undefined, + perlEnvAdd: true, severity5: "warning", severity4: "info", severity3: "hint", @@ -190,6 +193,19 @@ async function getWorkspaceFoldersSafe(): Promise<WorkspaceFolder[]> { } } +function expandTildePaths(paths: string, settings: NavigatorSettings): string { + const path = paths; + // Consider that this not a Windows feature, + // so, Windows "%USERPROFILE%" currently is ignored (and rarely used). + if (path.startsWith("~/")) { + const newPath = homedir() + path.slice(1); + nLog("Expanding tilde path '" + path + "' to '" + newPath + "'", settings); + return newPath; + } else { + return path; + } +} + async function getDocumentSettings(resource: string): Promise<NavigatorSettings> { if (!hasConfigurationCapability) { return globalSettings; @@ -202,6 +218,26 @@ async function getDocumentSettings(resource: string): Promise<NavigatorSettings> }); if (!result) return globalSettings; const resolvedSettings = { ...globalSettings, ...result }; + + if(resolvedSettings.includePaths) { + resolvedSettings.includePaths = resolvedSettings.includePaths.map((path: string) => expandTildePaths(path, resolvedSettings)); + } + if(resolvedSettings.perlPath) { + resolvedSettings.perlPath = expandTildePaths(resolvedSettings.perlPath, resolvedSettings); + } + if(resolvedSettings.perlimportsProfile) { + resolvedSettings.perlimportsProfile = expandTildePaths(resolvedSettings.perlimportsProfile, resolvedSettings); + } + if(resolvedSettings.perltidyProfile) { + resolvedSettings.perltidyProfile = expandTildePaths(resolvedSettings.perltidyProfile, resolvedSettings); + } + if(resolvedSettings.perlcriticProfile) { + resolvedSettings.perlcriticProfile = expandTildePaths(resolvedSettings.perlcriticProfile, resolvedSettings); + } + if(resolvedSettings.perlEnv) { + resolvedSettings.perlEnv = Object.fromEntries(Object.entries(resolvedSettings.perlEnv).map(([key, value]) => [key, expandTildePaths(value, resolvedSettings)])); + } + documentSettings.set(resource, resolvedSettings); return resolvedSettings; } diff --git a/server/src/types.ts b/server/src/types.ts index bf4394f..01ac28a 100644 --- a/server/src/types.ts +++ b/server/src/types.ts @@ -20,6 +20,8 @@ export interface NavigatorSettings { perltidyEnabled: boolean; perltidyProfile: string; perlCompileEnabled: boolean; + perlEnv: undefined | { [key: string]: string }; + perlEnvAdd: boolean; severity5: string; severity4: string; severity3: string; |