Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
75b036b
Add CLAUDE.md to provide guidance for Claude Code
lambdalisue Jul 5, 2025
6bdfbe0
feat(source): add window source for listing Vim windows
lambdalisue Jul 6, 2025
ad2d410
feat(source): add tabpage source for listing Vim tab pages
lambdalisue Jul 6, 2025
9004a59
feat(source): add loclist source for listing location list items
lambdalisue Jul 6, 2025
d337686
feat(source): add colorscheme source for listing available colorschemes
lambdalisue Jul 6, 2025
d8a627c
feat(source): add highlight source for listing highlight groups
lambdalisue Jul 6, 2025
33e58cd
feat(source): add jumplist source for listing jump locations
lambdalisue Jul 6, 2025
fd1a7c8
feat(source): add register source for listing Vim registers
lambdalisue Jul 6, 2025
5006534
feat(source): add mark source for listing Vim marks
lambdalisue Jul 6, 2025
1c0e825
feat(source): add command source for listing user-defined commands
lambdalisue Jul 6, 2025
9c39f46
feat(source): add mapping source for listing key mappings
lambdalisue Jul 6, 2025
e8f21f1
feat(source): add git status source for listing modified files
lambdalisue Jul 6, 2025
881a059
feat(source): add grep source for vim's :grep command results
lambdalisue Jul 6, 2025
c3bf51b
feat(source): add vimgrep source for vim's :vimgrep command results
lambdalisue Jul 6, 2025
89e0265
feat(source): add autocmd source for vim autocmds
lambdalisue Jul 6, 2025
c74961d
feat(previewer): add shell command previewer
lambdalisue Jul 6, 2025
bfba569
feat(renderer): add file info renderer for displaying file metadata
lambdalisue Jul 6, 2025
8090011
feat(renderer): add buffer info renderer for displaying buffer metadata
lambdalisue Jul 6, 2025
0f0a8f2
feat(renderer): add smart grep renderer for formatting grep-like results
lambdalisue Jul 6, 2025
e9a3319
feat(refiner): add file info refiner for filtering by file properties
lambdalisue Jul 6, 2025
c166b92
feat(refiner): add buffer info refiner for filtering by buffer proper…
lambdalisue Jul 6, 2025
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
166 changes: 166 additions & 0 deletions builtin/source/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import * as fn from "@denops/std/function";

import { defineSource, type Source } from "../../source.ts";

type Detail = {
/**
* Mapping key/lhs
*/
lhs: string;

/**
* Mapping value/rhs
*/
rhs: string;

/**
* Mapping mode
*/
mode: string;

/**
* Whether the mapping is buffer-local
*/
bufferLocal: boolean;

/**
* Whether the mapping is silent
*/
silent: boolean;

/**
* Whether the mapping is noremap
*/
noremap: boolean;

/**
* Whether the mapping waits for more input
*/
nowait: boolean;

/**
* Whether the mapping is an expression
*/
expr: boolean;
};

export type MappingOptions = {
/**
* Which modes to include mappings from.
* Default includes all common modes.
* @default ["n", "i", "v", "x", "s", "o", "c", "t"]
*/
modes?: string[];

/**
* Whether to include buffer-local mappings.
* @default true
*/
includeBufferLocal?: boolean;

/**
* Whether to include plugin mappings (mappings containing <Plug>).
* @default false
*/
includePluginMappings?: boolean;
};

/**
* Creates a Source that generates items from Vim key mappings.
*
* This Source retrieves all key mappings from specified modes and
* generates items for each one, showing their definitions and attributes.
*
* @param options - Options to customize mapping listing.
* @returns A Source that generates items representing mappings.
*/
export function mapping(
options: Readonly<MappingOptions> = {},
): Source<Detail> {
const modes = options.modes ?? ["n", "i", "v", "x", "s", "o", "c", "t"];
const includeBufferLocal = options.includeBufferLocal ?? true;
const includePluginMappings = options.includePluginMappings ?? false;

return defineSource(async function* (denops, _params, { signal }) {
const items: Array<{
id: number;
value: string;
detail: Detail;
}> = [];

let id = 0;
for (const mode of modes) {
// Get mappings for this mode
const mappingList = await fn.maplist(denops, mode) as Array<{
lhs: string;
rhs: string;
silent: number | boolean;
noremap: number | boolean;
nowait: number | boolean;
expr: number | boolean;
buffer: number | boolean;
mode?: string;
sid?: number;
lnum?: number;
script?: number | boolean;
}>;
Comment on lines +94 to +106
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using as. Try @core/unknownutil to use type guard.

signal?.throwIfAborted();

for (const mapping of mappingList) {
// Skip buffer-local mappings if not included
const isBufferLocal = Boolean(mapping.buffer);
if (isBufferLocal && !includeBufferLocal) {
continue;
}

// Skip plugin mappings if not included
if (!includePluginMappings && mapping.rhs.includes("<Plug>")) {
continue;
}

// Build attributes
const attributes: string[] = [];
if (mapping.silent) attributes.push("silent");
if (mapping.noremap) attributes.push("noremap");
if (mapping.nowait) attributes.push("nowait");
if (mapping.expr) attributes.push("expr");
if (isBufferLocal) attributes.push("buffer");

// Format mode indicator
const modeIndicator = mode === "n" ? " " : `[${mode}]`;

// Format display value
const attrStr = attributes.length > 0
? ` (${attributes.join(", ")})`
: "";
const truncatedRhs = mapping.rhs.length > 50
? mapping.rhs.substring(0, 47) + "..."
: mapping.rhs;

items.push({
id: id++,
value: `${modeIndicator} ${mapping.lhs}${attrStr} → ${truncatedRhs}`,
detail: {
lhs: mapping.lhs,
rhs: mapping.rhs,
mode: mode,
bufferLocal: isBufferLocal,
silent: Boolean(mapping.silent),
noremap: Boolean(mapping.noremap),
nowait: Boolean(mapping.nowait),
expr: Boolean(mapping.expr),
},
});
Comment on lines +140 to +153
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just yield.

}
}

// Sort by mode and then by lhs
items.sort((a, b) => {
const modeCmp = a.detail.mode.localeCompare(b.detail.mode);
if (modeCmp !== 0) return modeCmp;
return a.detail.lhs.localeCompare(b.detail.lhs);
});
Comment on lines +157 to +162
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should NOT sort in source.


yield* items;
});
}
1 change: 1 addition & 0 deletions builtin/source/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from "./jumplist.ts";
export * from "./line.ts";
export * from "./list.ts";
export * from "./loclist.ts";
export * from "./mapping.ts";
export * from "./mark.ts";
export * from "./noop.ts";
export * from "./oldfiles.ts";
Expand Down