Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dot-repeat #20

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions autoload/denippet.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ function denippet#expandable() abort
return s:request('expandable')
endfunction

function denippet#expand() abort
call s:request('expand')
function denippet#expand(...) abort
call s:notify('expand', a:000)
endfunction

function denippet#anonymous(body) abort
call s:request('anonymous', [a:body])
call s:notify('anonymous', [a:body])
endfunction

function denippet#jumpable(...) abort
Expand Down
6 changes: 4 additions & 2 deletions denops/@ddc-sources/denippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
GetPreviewerArguments,
OnCompleteDoneArguments,
} from "https://deno.land/x/[email protected]/base/source.ts";
import { Denops, op } from "../denippet/deps/denops.ts";
import { Denops, fn, op, q, useExprString } from "../denippet/deps/denops.ts";
import { splitLines } from "../denippet/util.ts";
import { lsputil } from "../denippet/deps/lsp.ts";

Expand Down Expand Up @@ -59,7 +59,9 @@ export class Source extends BaseSource<Params> {
}
}

await denops.dispatch("denippet", "expand", userData.denippet.id);
await useExprString(denops, async (denops) => {
await fn.feedkeys(denops, q`\<Cmd>call denippet#expand("${userData.denippet.id}")\<CR>`);
});
await denops.call("ddc#skip_next_complete");
}

Expand Down
24 changes: 15 additions & 9 deletions denops/denippet/deps/denops.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
export * as fn from "https://deno.land/x/[email protected]/function/mod.ts";
export * as api from "https://deno.land/x/[email protected]/function/nvim/mod.ts";
export * as vim from "https://deno.land/x/[email protected]/function/vim/mod.ts";
export * as op from "https://deno.land/x/[email protected]/option/mod.ts";
export { b, g } from "https://deno.land/x/[email protected]/variable/variable.ts";
export * as au from "https://deno.land/x/[email protected]/autocmd/mod.ts";
export * as lambda from "https://deno.land/x/[email protected]/lambda/mod.ts";
export { batch } from "https://deno.land/x/[email protected]/batch/mod.ts";
export type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
export * as fn from "https://deno.land/x/[email protected]/function/mod.ts";
export * as api from "https://deno.land/x/[email protected]/function/nvim/mod.ts";
export * as vim from "https://deno.land/x/[email protected]/function/vim/mod.ts";
export * as op from "https://deno.land/x/[email protected]/option/mod.ts";
export { b, g } from "https://deno.land/x/[email protected]/variable/variable.ts";
export * as au from "https://deno.land/x/[email protected]/autocmd/mod.ts";
export * as lambda from "https://deno.land/x/[email protected]/lambda/mod.ts";
export { batch } from "https://deno.land/x/[email protected]/batch/mod.ts";
export * as Keymap from "https://deno.land/x/[email protected]/helper/keymap.ts";
export {
exprQuote as q,
type ExprString,
useExprString,
} from "https://deno.land/x/[email protected]/helper/expr_string.ts";

export { test } from "https://deno.land/x/[email protected]/mod.ts";
9 changes: 5 additions & 4 deletions denops/denippet/deps/std.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as TOML from "https://deno.land/[email protected]/toml/mod.ts";
export * as YAML from "https://deno.land/[email protected]/yaml/mod.ts";
export * as path from "https://deno.land/[email protected]/path/mod.ts";
export { assert, assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
export * as TOML from "https://deno.land/[email protected]/toml/mod.ts";
export * as YAML from "https://deno.land/[email protected]/yaml/mod.ts";
export * as path from "https://deno.land/[email protected]/path/mod.ts";
export { assert, assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
export { deadline } from "https://deno.land/[email protected]/async/mod.ts";
4 changes: 2 additions & 2 deletions denops/denippet/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function main(denops: Denops): void {
const filepath = u.ensure(filepathU, is.String);
const filetype = u.ensure(
filetypeU,
is.OneOf([is.String, is.ArrayOf(is.String)]),
is.UnionOf([is.String, is.ArrayOf(is.String)]),
);
try {
await loader.load(filepath, filetype);
Expand Down Expand Up @@ -135,7 +135,7 @@ export function main(denops: Denops): void {
},

async anonymous(bodyU: unknown, prefixU: unknown): Promise<void> {
const body = normalizeNewline(u.ensure(bodyU, is.OneOf([is.String, is.ArrayOf(is.String)])));
const body = normalizeNewline(u.ensure(bodyU, is.UnionOf([is.String, is.ArrayOf(is.String)])));
const prefix = u.ensure(prefixU, is.OptionalOf(is.String));
if (await session.expand(body, prefix)) {
const syncDelay = Number(await g.get(denops, "denippet_sync_delay"));
Expand Down
65 changes: 6 additions & 59 deletions denops/denippet/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { batch, Denops } from "./deps/denops.ts";
import { LSP, lsputil } from "./deps/lsp.ts";
import { batch, Denops, Keymap, q } from "./deps/denops.ts";
import { is } from "./deps/unknownutil.ts";
import { getExtmarks, setExtmark } from "./extmark.ts";

export async function echoerr(
denops: Denops,
Expand Down Expand Up @@ -34,11 +32,6 @@ export function splitLines(text: string): string[] {
return normalizeNewline(text).split("\n");
}

const ENCODER = new TextEncoder();
function byteLength(str: string): number {
return ENCODER.encode(str).length;
}

/**
* Fix extmarks position
*
Expand All @@ -58,55 +51,9 @@ export async function linePatch(
after: number,
insertText: string,
): Promise<void> {
const cursor16 = await lsputil.getCursor(denops);
const cursor = await lsputil.toUtf8Position(denops, 0, cursor16, "utf-16");
const lines = splitLines(insertText);

function shift(pos: LSP.Position): LSP.Position {
if (lines.length > 1) {
return {
line: pos.line + lines.length - 1,
character: byteLength(lines[lines.length - 1]) + pos.character - cursor.character - after,
};
} else {
return {
line: pos.line,
character: pos.character - before - after + byteLength(insertText),
};
}
}

// Save and adjust extmarks
const extmarks = (await getExtmarks(denops, cursor.line)).map((mark) => {
let start = mark.range.start;
let end = mark.range.end;

if (start.character > cursor.character + after) {
// 6
start = shift(start);
} else if (start.character > cursor.character - before) {
// 4 and 5
start.character = cursor.character - before;
}

const beforePos = { ...cursor };
beforePos.character -= before;
const afterPos = { ...cursor };
afterPos.character += after;
if (lsputil.isPositionBefore(afterPos, end)) {
// 3, 5 and 6
end = shift(end);
} else if (lsputil.isPositionBefore(beforePos, end, true)) {
// 2 and 4
end = shift(afterPos);
}
return { ...mark, range: { start, end } };
});

await lsputil.linePatch(denops, before, after, insertText);

// Restore extmarks
for (const mark of extmarks) {
await setExtmark(denops, mark.range, mark.extmarkId);
}
await Keymap.send(denops, [
q`${q`\<C-g>U\<Left>\<Del>`.repeat(before)}`,
q`${q`\<Del>`.repeat(after)}`,
q`${insertText.replaceAll("\n", "\n\\<C-u>")}`,
]);
}
5 changes: 2 additions & 3 deletions doc/denippet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ denippet#jump([{dir}]) *denippet#jump()*
Same as |<Plug>(denippet-jump-next)| and |<Plug>(denippet-jump-prev)|.


denippet#choosable([{dir}]) *denippet#choosable()*
{dir} where 1 means "next" and -1 means "previous".
Return |v:true| if choice is possible, |v:false| otherwise.
denippet#choosable() *denippet#choosable()*
Return |v:true| if current node is choiceNode, |v:false| otherwise.


denippet#choice([{dir}]) *denippet#choice()*
Expand Down
Loading