UNPKG

fish-lsp

Version:

LSP implementation for fish/fish-shell

178 lines (177 loc) 7.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LspCommands = exports.CommandNames = void 0; exports.createExecuteCommandHandler = createExecuteCommandHandler; const exec_1 = require("./utils/exec"); const snippets_1 = require("./utils/snippets"); const execute_handler_1 = require("./execute-handler"); // Define command name constants to avoid string literals exports.CommandNames = { // SHOW_REFERENCES: 'fish-lsp.showReferences', EXECUTE_RANGE: 'fish-lsp.executeRange', EXECUTE_LINE: 'fish-lsp.executeLine', EXECUTE: 'fish-lsp.execute', EXECUTE_BUFFER: 'fish-lsp.executeBuffer', CREATE_THEME: 'fish-lsp.createTheme', // OPEN_SAVED_FUNCTION: 'fish-lsp.openSavedFunction', // OPEN_COMPLETIONS: 'fish-lsp.openCompletions', // RUN_TEST: 'fish-lsp.runTest', SHOW_STATUS_DOCS: 'fish-lsp.showStatusDocs', // OPEN_SOURCE: 'fish-lsp.openSource', // SHOW_COMMAND_DOCS: 'fish-lsp.showCommandDocs', }; exports.LspCommands = [...Array.from(Object.values(exports.CommandNames))]; // Function to create the command handler with dependencies injected function createExecuteCommandHandler(connection, docs, logger) { // Individual command implementations // async function handleShowReferences(uri: string, position: Position, references: Location[]) { // await connection.sendRequest('textDocument/references', { // textDocument: { uri }, // position, // context: { includeDeclaration: true } // }); // } // // async function openSavedFunction(path: string) { // await connection.sendRequest('window/showDocument', { // uri: `file://${path}`, // takeFocus: true, // }); // } // async function openCompletions(path: string) { // let commandName = path; // // if (path.endsWith('.fish')) { // commandName = path.split('/').pop() || commandName; // commandName = commandName?.split('.fish').at(0) || commandName; // } // const results = (await execAsyncF(`path sort --unique --key=basename $fish_complete_path/*.fish | string match -e '/${commandName}.fish'`)).split('\n'); // for (const result of results) { // await connection.sendRequest('window/showDocument', { // uri: `file://${result}`, // takeFocus: true, // }); // } // } async function executeRange(uri, range) { logger.log('executeRange', uri, range); const doc = docs.get(uri); if (!doc) return; const text = doc.getText(range); const output = await (0, exec_1.execAsyncF)(text); logger.log('onExecuteCommand', text); logger.log('onExecuteCommand', output); const response = (0, execute_handler_1.buildExecuteNotificationResponse)(text, { stdout: output, stderr: '' }); (0, execute_handler_1.useMessageKind)(connection, response); } async function executeLine(uri, line) { logger.log('executeLine', uri, line); const doc = docs.get(uri); if (!doc) return; const numberLine = Number.parseInt(line.toString()) - 1; const text = doc.getLine(numberLine); const cmdOutput = await (0, exec_1.execAsyncF)(`${text}; echo "\\$status: $status"`); logger.log('executeLine.cmdOutput', cmdOutput); const output = (0, execute_handler_1.buildExecuteNotificationResponse)(text, { stdout: cmdOutput, stderr: '' }); logger.log('onExecuteCommand', text); logger.log('onExecuteCommand', output); // const response = buildExecuteNotificationResponse(text, ); (0, execute_handler_1.useMessageKind)(connection, output); } async function createTheme(themeName, asVariables = true) { const path = `~/.config/fish/themes/${themeName}.fish`; const output = (await (0, exec_1.execAsyncFish)('fish_config theme dump; or true')).stdout.split('\n'); const outputArr = []; // Append the longest line to the file if (asVariables) { outputArr.push('# created by fish-lsp'); } for (const line of output) { if (asVariables) { outputArr.push(`set -gx ${line}`); } else { outputArr.push(`${line}`); } } const outputStr = outputArr.join('\n'); await connection.sendRequest('workspace/applyEdit', { changes: { [path]: outputStr, }, }); await connection.sendRequest('window/showDocument', { uri: `file://${path}`, takeFocus: true, }); (0, execute_handler_1.useMessageKind)(connection, { message: `${execute_handler_1.fishLspPromptIcon} appended theme variables to end of file`, kind: 'info', }); } async function executeBuffer(path) { const output = await (0, execute_handler_1.execEntireBuffer)(path); // Append the longest line to the file (0, execute_handler_1.useMessageKind)(connection, output); } // async function handleRunTest(uri: string, range: Range) { // const doc = docs.get(uri); // if (!doc) return; // // const functionText = doc.getText(range); // const output = await execAsyncFish(functionText); // // if (output.stderr) { // connection.window.showErrorMessage(`Test failed: ${output.stderr}`); // } else { // connection.window.showInformationMessage(`Test passed: ${output.stdout}`); // } // } function handleShowStatusDocs(statusCode) { const statusInfo = snippets_1.PrebuiltDocumentationMap.getByType('status') .find(item => item.name === statusCode); if (statusInfo) { connection.window.showInformationMessage(statusInfo.description); } } // async function handleOpenSource(sourcePath: string) { // // await connection.workspace.getWorkspaceFolders() // await connection.sendRequest('workspace/openFile', { // uri: pathToUri(sourcePath) // }); // } // // async function handleShowCommandDocs(commandName: string) { // const docs = await execCommandDocs(commandName); // if (docs) { // connection.window.showInformationMessage(docs); // } // } // Command handler mapping const commandHandlers = { // 'fish-lsp.showReferences': handleShowReferences, 'fish-lsp.executeRange': executeRange, 'fish-lsp.executeLine': executeLine, 'fish-lsp.executeBuffer': executeBuffer, 'fish-lsp.execute': executeBuffer, 'fish-lsp.createTheme': createTheme, // 'fish-lsp.openSavedFunction': openSavedFunction, // 'fish-lsp.openCompletions': openCompletions, // 'fish-lsp.runTest': handleRunTest, 'fish-lsp.showStatusDocs': handleShowStatusDocs, // 'fish-lsp.openSource': handleOpenSource, // 'fish-lsp.showCommandDocs': handleShowCommandDocs, }; // Main command handler function return async function onExecuteCommand(params) { logger.log('onExecuteCommand', params); const handler = commandHandlers[params.command]; if (!handler) { logger.log(`Unknown command: ${params.command}`); return; } await handler(...params.arguments || []); }; }