|
| 1 | +import type { Action } from "https://deno.land/x/fall_core@v0.3.0/mod.ts"; |
| 2 | +import * as fn from "https://deno.land/x/denops_std@v6.3.0/function/mod.ts"; |
| 3 | +import { assert, is } from "https://deno.land/x/unknownutil@v3.16.3/mod.ts"; |
| 4 | + |
| 5 | +const isOptions = is.StrictOf(is.PartialOf(is.ObjectOf({ |
| 6 | + what: is.PartialOf(is.ObjectOf({ |
| 7 | + context: is.Unknown, |
| 8 | + id: is.Number, |
| 9 | + idx: is.UnionOf([is.Number, is.String]), |
| 10 | + nr: is.Number, |
| 11 | + title: is.String, |
| 12 | + })), |
| 13 | + action: is.LiteralOneOf(["a", "r", "f", " "] as const), |
| 14 | + target: is.LiteralOneOf( |
| 15 | + [ |
| 16 | + "selected-or-cursor", |
| 17 | + "selected-or-processed", |
| 18 | + ] as const, |
| 19 | + ), |
| 20 | +}))); |
| 21 | + |
| 22 | +const isPathDetail = is.ObjectOf({ |
| 23 | + path: is.String, |
| 24 | + line: is.OptionalOf(is.Number), |
| 25 | + column: is.OptionalOf(is.Number), |
| 26 | +}); |
| 27 | + |
| 28 | +function isDefined<T>(value: T | undefined): value is T { |
| 29 | + return value !== undefined; |
| 30 | +} |
| 31 | + |
| 32 | +export function getAction( |
| 33 | + options: Record<string, unknown>, |
| 34 | +): Action { |
| 35 | + assert(options, isOptions); |
| 36 | + const what = options.what ?? {}; |
| 37 | + const action = options.action ?? " "; |
| 38 | + const target = options.target ?? "selected-or-cursor"; |
| 39 | + return { |
| 40 | + invoke: async (denops, { cursorItem, selectedItems, processedItems }) => { |
| 41 | + const source = selectedItems.length > 0 |
| 42 | + ? selectedItems |
| 43 | + : target === "selected-or-cursor" |
| 44 | + ? cursorItem ? [cursorItem] : [] |
| 45 | + : processedItems; |
| 46 | + const items = source |
| 47 | + .map((item) => { |
| 48 | + if (isPathDetail(item.detail)) { |
| 49 | + return { |
| 50 | + filename: item.detail.path, |
| 51 | + lnum: item.detail.line, |
| 52 | + col: item.detail.column, |
| 53 | + }; |
| 54 | + } |
| 55 | + return undefined; |
| 56 | + }) |
| 57 | + .filter(isDefined); |
| 58 | + try { |
| 59 | + await fn.setqflist(denops, [], action, { |
| 60 | + ...what, |
| 61 | + items, |
| 62 | + }); |
| 63 | + } catch (err) { |
| 64 | + // Fail silently |
| 65 | + console.debug( |
| 66 | + `[fall] (action/quickfix) Failed to set quickfix list:`, |
| 67 | + err, |
| 68 | + ); |
| 69 | + } |
| 70 | + return false; |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
0 commit comments