Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/launch-editor/editor-info/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
}
2 changes: 2 additions & 0 deletions packages/launch-editor/get-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions packages/launch-editor/get-args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe('getArgumentsForFile', () => {
'wstorm',
'charm',
'zed',
'zeditor',
'zedit',
]) {
test(`${editor} uses "file:line:column"`, () => {
assert.deepEqual(getArgumentsForFile(editor, absoluteFileName, 10, 5), [
Expand Down
31 changes: 29 additions & 2 deletions packages/launch-editor/guess.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs')
const path = require('path')
const shellQuote = require('shell-quote')
const childProcess = require('child_process')
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions packages/launch-editor/guess.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down