UNPKG

coc-autark

Version:

Autark build-script completion for coc.nvim

71 lines (63 loc) • 2.02 kB
import path from 'node:path'; import { provideCompletions } from './completion.js'; // coc.nvim-facing glue with the API injected as `api` ({languages, workspace, // Uri}), so the registration logic stays testable without a coc runtime. const EXTENSION_NAME = 'coc-autark'; const SOURCE_NAME = 'coc-autark'; const SOURCE_SHORTCUT = 'AUTARK'; const TRIGGER_CHARACTERS = ['{', '$', '/', '-']; function uriToPath(api, uri) { if (!uri) { return ''; } try { return api.Uri.parse(uri).fsPath; } catch (_) { return ''; } } function readConfig(api) { const cfg = api.workspace.getConfiguration('autark'); return { cacheDir: cfg.get('cacheDir', 'autark-cache'), checkScriptDirs: cfg.get('checkScriptDirs', ['.autark', 'autark']), completePaths: cfg.get('completePaths', true), }; } function createProvider(context, api) { return { provideCompletionItems(document, position) { const filePath = uriToPath(api, document.uri); try { return provideCompletions({ text: document.getText(), offset: document.offsetAt(position), filePath, workspaceRoot: api.workspace.root || (filePath ? path.dirname(filePath) : ''), config: readConfig(api), }); } catch (error) { context.logger.error(`completion failed: ${error && error.stack ? error.stack : error}`); return []; } }, }; } /** Registers the completion source; `api` is the coc.nvim module namespace. */ export function activateExtension(context, api) { const cfg = api.workspace.getConfiguration('autark'); if (cfg.get('enabled', true) === false) { return; } const filetypes = cfg.get('filetypes', ['autark']); context.logger.info(`Activated ${EXTENSION_NAME} for ${filetypes.join(', ')}`); context.subscriptions.push( api.languages.registerCompletionItemProvider( SOURCE_NAME, SOURCE_SHORTCUT, filetypes, createProvider(context, api), TRIGGER_CHARACTERS ) ); }