Skip to content

Commit

Permalink
feat: add quickfix action, fix #585
Browse files Browse the repository at this point in the history
  • Loading branch information
weirongxu committed Jun 8, 2024
1 parent 4faf82e commit 8e2f28f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ Type: <pre><code>string</code></pre>Default: <pre><code>"[icon] [title] [hidden
</details>
<details>
<summary><code>explorer.buffer.child.template</code>: Template for child node of buffer source.</summary>
Type: <pre><code>string</code></pre>Default: <pre><code>"[git | 2] [selection | 1] [bufnr] [name][modified][readonly] [fullpath]"</code></pre>
Type: <pre><code>string</code></pre>Default: <pre><code>"[git | 2] [selection | 1] [bufnr] [name][modified][readonly] [relativePath]"</code></pre>
</details>
<details>
<summary><code>explorer.buffer.child.labelingTemplate</code>: Labeling template for child node of buffer source, use for preview when previewAction is labeling.</summary>
Expand Down
27 changes: 12 additions & 15 deletions src/actions/actionSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export class ActionSource<
if (Array.isArray(actionExp)) {
for (let i = 0; i < actionExp.length; i++) {
if (i !== 0) {
curNodes = [this.source.view.currentNode()];
const curNode = this.source.view.currentNode();
curNodes = curNode ? [curNode] : [];
}

const action = actionExp[i];
Expand Down Expand Up @@ -134,6 +135,9 @@ export class ActionSource<

async doAction(
name: string,
/**
* visual nodes
*/
nodes: TreeNode | TreeNode[],
args: string[] = [],
mode: MappingMode = 'n',
Expand All @@ -150,7 +154,10 @@ export class ActionSource<
try {
switch (select) {
case true: {
const allNodes = uniq([...finalNodes, ...source.selectedNodes]);
const allNodes: TreeNode[] = uniq([
...finalNodes,
...source.selectedNodes,
]);
source.selectedNodes.clear();
source.view.requestRenderNodes(allNodes);
await action.callback.call(source, {
Expand Down Expand Up @@ -212,7 +219,7 @@ export class ActionSource<
[...actions.entries()]
.filter(([actionName]) => actionName !== 'actionMenu')
.sort(([aName], [bName]) => aName.localeCompare(bName))
.map(([actionName, { callback, options, description }]) => {
.map(([actionName, { options, description }]) => {
const keys = reverseMappings.get(actionName);
const key = keys ? keys.vmap ?? keys.nmap : '';
const list = [
Expand All @@ -222,12 +229,7 @@ export class ActionSource<
description,
callback: async () => {
await task.waitExplorerShow();
await callback.call(source, {
source,
nodes,
args: [],
mode: 'n',
});
await source.action.doAction(actionName, nodes, [], 'n');
},
},
];
Expand All @@ -243,12 +245,7 @@ export class ActionSource<
description: `${description} ${menu.description}`,
callback: async () => {
await task.waitExplorerShow();
await callback.call(source, {
source,
nodes,
args: await menu.actionArgs(),
mode: 'n',
});
await source.action.doAction(actionName, nodes, [], 'n');
},
};
}),
Expand Down
42 changes: 42 additions & 0 deletions src/actions/globalActions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { compactI } from 'coc-helper';
import { workspace } from 'coc.nvim';
import pathLib from 'path';
import { gitManager } from '../git/manager';
import { parseOriginalActionExp } from '../mappings';
import {
Expand All @@ -9,6 +10,7 @@ import {
openStrategyList,
previewOnHoverActionList,
previewStrategyList,
quickfixActions,
textobjTargetList,
textobjTypeList,
type CollapseOption,
Expand All @@ -17,6 +19,7 @@ import {
type OpenCursorPosition,
type OpenStrategy,
type PreviewOnHoverAction,
type QuickfixAction,
type TextobjTarget,
} from '../types';
import type { PreviewActionStrategy } from '../types/pkg-config';
Expand Down Expand Up @@ -776,6 +779,45 @@ export function loadGlobalActions(action: ActionExplorer) {
select: 'visual',
},
);
action.addNodesAction(
'quickfix',
async ({ args, nodes, source }) => {
const argAction = (args[0] ?? 'replace') as QuickfixAction;
const action = { add: 'a', replace: 'r' }[argAction];
await nvim.call('setqflist', [
nodes
.filter((it) => it.fullpath && !it.expandable)
.map((it) => {
const realtive = pathLib.relative(source.root, it.fullpath!);
return {
filename: it.fullpath,
text: realtive,
};
}),
action,
]);
const openCommand = (await nvim.getVar(
'coc_quickfix_open_command',
)) as string;
await nvim.command(
typeof openCommand === 'string' ? openCommand : 'copen',
);
},
'push nodes to quickfix list',
{
select: true,
args: [
{
name: 'action',
description: `action for quickfix, ${quickfixActions.join(' | ')}`,
},
],
menus: {
add: 'add nodes to quickfix list',
replace: 'replace nodes in quickfix list',
},
},
);
action.addNodeAction(
'normal',
async ({ args }) => {
Expand Down
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export const openStrategyList: OpenStrategy[] = [
'sourceWindow',
];

export const quickfixActions = ['add', 'replace'] as const;

export type QuickfixAction = (typeof quickfixActions)[number];

/**
* `keep string` - Keep cursor in explorer when open
* `position object` - Open cursor in special position
Expand Down

0 comments on commit 8e2f28f

Please sign in to comment.