diff --git a/packages/launch-editor/editor-info/linux.js b/packages/launch-editor/editor-info/linux.js index c93614d..51da6db 100644 --- a/packages/launch-editor/editor-info/linux.js +++ b/packages/launch-editor/editor-info/linux.js @@ -26,5 +26,7 @@ module.exports = { 'goland.sh': 'goland', rider: 'rider', 'rider.sh': 'rider', - zed: 'zed', + // The official binary is `zed`. Some distros rename it to avoid a package + // name conflict: https://zed.dev/docs/linux#installing-via-a-package-manager + zed: ['zeditor', 'zedit', 'zed'], } diff --git a/packages/launch-editor/get-args.js b/packages/launch-editor/get-args.js index 13e7506..143e87d 100644 --- a/packages/launch-editor/get-args.js +++ b/packages/launch-editor/get-args.js @@ -34,6 +34,8 @@ module.exports = function getArgumentsForFile(editor, fileName, lineNumber, colu case 'wstorm': case 'charm': case 'zed': + case 'zeditor': + case 'zedit': return [`${fileName}:${lineNumber}:${columnNumber}`] case 'notepad++': return ['-n' + lineNumber, '-c' + columnNumber, fileName] diff --git a/packages/launch-editor/get-args.test.js b/packages/launch-editor/get-args.test.js index 395b86a..9889e67 100644 --- a/packages/launch-editor/get-args.test.js +++ b/packages/launch-editor/get-args.test.js @@ -57,6 +57,8 @@ describe('getArgumentsForFile', () => { 'wstorm', 'charm', 'zed', + 'zeditor', + 'zedit', ]) { test(`${editor} uses "file:line:column"`, () => { assert.deepEqual(getArgumentsForFile(editor, absoluteFileName, 10, 5), [ diff --git a/packages/launch-editor/guess.js b/packages/launch-editor/guess.js index 3bcba92..586bc5f 100644 --- a/packages/launch-editor/guess.js +++ b/packages/launch-editor/guess.js @@ -1,3 +1,4 @@ +const fs = require('fs') const path = require('path') const shellQuote = require('shell-quote') const childProcess = require('child_process') @@ -51,12 +52,38 @@ function getEditorFromWindowsProcesses(output) { return undefined } -function getEditorFromLinuxProcesses(output) { +function isExecutableOnPath(command) { + const pathDirs = (process.env.PATH || '').split(path.delimiter) + return pathDirs.some((pathDir) => { + if (!pathDir) { + return false + } + try { + fs.accessSync(path.join(pathDir, command), fs.constants.X_OK) + return true + } catch { + return false + } + }) +} + +// A mapping is either a single command or a list of candidates in preference +// order. The last candidate is the canonical command, used when none of them +// exist in PATH. +function resolveCommand(commands, isExecutable) { + if (typeof commands === 'string') { + return commands + } + const found = commands.find((command) => isExecutable(command)) + return found || commands.at(-1) +} + +function getEditorFromLinuxProcesses(output, isExecutable = isExecutableOnPath) { const processNames = Object.keys(COMMON_EDITORS_LINUX) for (let i = 0; i < processNames.length; i++) { const processName = processNames[i] if (output.indexOf(processName) !== -1) { - return COMMON_EDITORS_LINUX[processName] + return resolveCommand(COMMON_EDITORS_LINUX[processName], isExecutable) } } return undefined diff --git a/packages/launch-editor/guess.test.js b/packages/launch-editor/guess.test.js index b285598..5e0a309 100644 --- a/packages/launch-editor/guess.test.js +++ b/packages/launch-editor/guess.test.js @@ -60,6 +60,32 @@ describe('getEditorFromLinuxProcesses', () => { assert.equal(getEditorFromLinuxProcesses(output), 'subl') }) + test('uses zeditor when a distro renamed the zed CLI', () => { + const output = ['systemd', 'zed', 'bash'].join('\n') + const isExecutable = (command) => command === 'zeditor' + assert.equal(getEditorFromLinuxProcesses(output, isExecutable), 'zeditor') + }) + + test('uses zedit when only that rename exists', () => { + const output = ['systemd', 'zed'].join('\n') + const isExecutable = (command) => command === 'zedit' + assert.equal(getEditorFromLinuxProcesses(output, isExecutable), 'zedit') + }) + + test('falls back to zed when no renamed CLI exists', () => { + const output = ['systemd', 'zed'].join('\n') + assert.equal( + getEditorFromLinuxProcesses(output, () => false), + 'zed', + ) + }) + + test('does not look up the command for a single-command mapping', () => { + const output = ['systemd', 'code'].join('\n') + const isExecutable = () => assert.fail('should not be called') + assert.equal(getEditorFromLinuxProcesses(output, isExecutable), 'code') + }) + test('matches code-insiders before code due to key order', () => { const output = ['systemd', 'code-insiders'].join('\n') assert.equal(getEditorFromLinuxProcesses(output), 'code-insiders')