Skip to content

Commit

Permalink
use modifier instead of some "Vnew"/"New" commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoh86 committed Apr 13, 2024
1 parent 6817e40 commit 6417044
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 55 deletions.
4 changes: 2 additions & 2 deletions autoload/docbase/buffer_action/posts_list.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function! docbase#buffer_action#posts_list#open(lnum, opener = "edit")
call denops#notify("docbase", "bufferAction", [bufnr(), "open", {"lnum": a:lnum, "opener": a:opener}])
function! docbase#buffer_action#posts_list#open(lnum, mods="")
call denops#notify("docbase", "bufferAction", [bufnr(), "open", {"lnum": a:lnum, "mods": a:mods}])
endfunction

function! docbase#buffer_action#posts_list#prev()
Expand Down
4 changes: 2 additions & 2 deletions autoload/docbase/buffer_action/teams_list.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function! docbase#buffer_action#teams_list#open(lnum, opener = "edit")
call denops#notify("docbase", "bufferAction", [bufnr(), "open", {"lnum": a:lnum, "opener": a:opener}])
function! docbase#buffer_action#teams_list#open(lnum, mods="")
call denops#notify("docbase", "bufferAction", [bufnr(), "open", {"lnum": a:lnum, "mods": a:mods}])
endfunction
16 changes: 8 additions & 8 deletions autoload/docbase/open.vim
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
function! docbase#open#teams_list(opener="edit")
call denops#notify("docbase", "openBuffer", ["TeamList", a:opener])
function! docbase#open#teams_list(mods="")
call denops#notify("docbase", "openBuffer", ["TeamList", a:mods])
endfunction

function! docbase#open#posts_list(domain, opener="edit")
call denops#notify("docbase", "openBuffer", ["PostList", {"domain": a:domain}, a:opener])
function! docbase#open#posts_list(domain, mods="")
call denops#notify("docbase", "openBuffer", ["PostList", {"domain": a:domain}, a:mods])
endfunction

function! docbase#open#post(domain, post_id, opener="edit")
call denops#notify("docbase", "openBuffer", ["Post", {"domain": a:domain,"postId": a:post_id}, a:opener])
function! docbase#open#post(domain, post_id, mods="")
call denops#notify("docbase", "openBuffer", ["Post", {"domain": a:domain,"postId": a:post_id}, a:mods])
endfunction

function! docbase#open#new_post(domain, opener="edit")
call denops#notify("docbase", "openBuffer", ["NewPost", {"domain": a:domain}, a:opener])
function! docbase#open#new_post(domain, mods="")
call denops#notify("docbase", "openBuffer", ["NewPost", {"domain": a:domain}, a:mods])
endfunction


10 changes: 2 additions & 8 deletions autoload/docbase/setup.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ function! docbase#setup#commands()

augroup docbase-setup-commands
autocmd!
autocmd Filetype docbase_teams_list command! <buffer> DocbaseOpenTeam call docbase#buffer_action#teams_list#open(line("."))
autocmd Filetype docbase_teams_list command! <buffer> DocbaseOpenTeamNew call docbase#buffer_action#teams_list#open(line("."), "new")
autocmd Filetype docbase_teams_list command! <buffer> DocbaseOpenTeamVnew call docbase#buffer_action#teams_list#open(line("."), "vnew")
autocmd Filetype docbase_teams_list command! <buffer> DocbaseOpenTeamTabedit call docbase#buffer_action#teams_list#open(line("."), "tabedit")
autocmd Filetype docbase_teams_list command! <buffer> DocbaseOpenTeam call docbase#buffer_action#teams_list#open(line("."), <q-mods>)

autocmd Filetype docbase_posts_list command! DocbaseOpenPost call docbase#buffer_action#posts_list#open(line("."))
autocmd Filetype docbase_posts_list command! DocbaseOpenPostNew call docbase#buffer_action#posts_list#open(line("."), "new")
autocmd Filetype docbase_posts_list command! DocbaseOpenPostVnew call docbase#buffer_action#posts_list#open(line("."), "vnew")
autocmd Filetype docbase_posts_list command! DocbaseOpenPostTabedit call docbase#buffer_action#posts_list#open(line("."), "tabedit")
autocmd Filetype docbase_posts_list command! DocbaseOpenPost call docbase#buffer_action#posts_list#open(line("."), <q-mods>)
autocmd Filetype docbase_posts_list command! DocbasePrevPage call docbase#buffer_action#posts_list#prev()
autocmd Filetype docbase_posts_list command! DocbaseNextPage call docbase#buffer_action#posts_list#next()
augroup END
Expand Down
6 changes: 3 additions & 3 deletions denops/docbase/handler/posts_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
maybe,
} from "https://deno.land/x/[email protected]/mod.ts";

import { isOpener, isPost } from "../types.ts";
import { isPost } from "../types.ts";
import { Filetype, prepareViewer, setViewerContent } from "./buffer.ts";
import { Handler, openBuffer } from "../router.ts";
import type { Context, Params } from "../router.ts";
Expand Down Expand Up @@ -86,7 +86,7 @@ export const PostList: Handler = {
async open(denops: Denops, _context: Context, _params: Params) {
const params = ensure(
_params,
is.ObjectOf({ lnum: is.Number, opener: is.OptionalOf(isOpener) }),
is.ObjectOf({ lnum: is.Number, mods: is.OptionalOf(is.String) }),
);
const domain = ensure(
await variable.b.get(denops, "docbase_posts_list_domain"),
Expand All @@ -102,7 +102,7 @@ export const PostList: Handler = {
await openBuffer(denops, "Post", {
domain,
postId: String(postIds[params.lnum - 1]),
}, params.opener);
}, params.mods);
},
prev(denops: Denops, context: Context, _params: Params) {
return paging(denops, context, -1);
Expand Down
5 changes: 2 additions & 3 deletions denops/docbase/handler/teams_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as variable from "https://deno.land/x/[email protected]/variable/variab
import { Filetype, prepareViewer, setViewerContent } from "./buffer.ts";
import { Handler, openBuffer } from "../router.ts";
import type { Context, Params } from "../router.ts";
import { isOpener } from "../types.ts";

const pattern = new URLPattern({
hostname: "teams",
Expand Down Expand Up @@ -39,7 +38,7 @@ export const TeamList: Handler = {
async open(denops: Denops, _context: Context, _params: Params) {
const params = ensure(
_params,
is.ObjectOf({ lnum: is.Number, opener: is.OptionalOf(isOpener) }),
is.ObjectOf({ lnum: is.Number, mods: is.OptionalOf(is.String) }),
);
const domains = ensure(
await variable.b.get(
Expand All @@ -52,7 +51,7 @@ export const TeamList: Handler = {
denops,
"PostList",
{ domain: domains[params.lnum - 1] },
params.opener,
params.mods,
);
},
},
Expand Down
8 changes: 4 additions & 4 deletions denops/docbase/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
setup,
} from "https://deno.land/[email protected]/log/mod.ts";

import { isOpener, isSearchPostsParams } from "./types.ts";
import { isSearchPostsParams } from "./types.ts";
import { Client } from "./api/client.ts";
import { bufferAction, bufferLoaded, openBuffer } from "./router.ts";
import { XDGStateMan } from "./state.ts";
Expand Down Expand Up @@ -46,12 +46,12 @@ export async function main(denops: Denops) {
});

denops.dispatcher = {
async openBuffer(uHandler: unknown, uProps: unknown, uOpener: unknown) {
async openBuffer(uHandler: unknown, uProps: unknown, uMods: unknown) {
try {
const handler = ensure(uHandler, is.String);
const props = ensure(uProps, is.Record);
const opener = ensure(uOpener, is.OptionalOf(isOpener));
await openBuffer(denops, handler, props, opener);
const mods = ensure(uMods, is.OptionalOf(is.String));
await openBuffer(denops, handler, props, mods);
} catch (err) {
getLogger("denops-docbase").error(err);
}
Expand Down
41 changes: 38 additions & 3 deletions denops/docbase/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
import * as fn from "https://deno.land/x/[email protected]/function/mod.ts";

import type { Opener } from "./types.ts";
import type { StateMan } from "./state.ts";
import { TeamList } from "./handler/teams_list.ts";
import { PostList } from "./handler/posts_list.ts";
Expand Down Expand Up @@ -45,18 +44,54 @@ function routing(bufname: string) {
throw new Error(`There's no valid handler for ${bufname}`);
}

function modsOpener(mods: string): string {
for (const mod of mods.split(/\s+/).reverse()) {
switch (mod) {
case "vert":
return "vnew";
case "verti":
return "vnew";
case "vertic":
return "vnew";
case "vertica":
return "vnew";
case "vertical":
return "vnew";
case "hor":
return "new";
case "hori":
return "new";
case "horiz":
return "new";
case "horizo":
return "new";
case "horizon":
return "new";
case "horizont":
return "new";
case "horizonta":
return "new";
case "horizontal":
return "new";
case "tab":
return "tabnew";
}
}
return "edit";
}
export async function openBuffer(
denops: Denops,
handler: string,
props: Record<string, unknown>,
opener: Opener = "edit",
mods: string = "",
) {
const h = handlers[handler];
if (!h) {
throw new Error(`There's no handler ${handler}`);
}
const bufname = h.bufname(props);
await denops.cmd(`${opener} ${bufname}`);
const opener = modsOpener(mods);
await denops.cmd(`${mods} ${opener} ${bufname}`);
}

export async function bufferLoaded(
Expand Down
14 changes: 0 additions & 14 deletions denops/docbase/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
import {
is,
// ObjectOf as O,
Predicate as P,
} from "https://deno.land/x/[email protected]/mod.ts";

export type Opener = "edit" | "new" | "vnew" | "tabnew";
export const isOpener: P<Opener> = is.OneOf([
is.LiteralOf("edit"),
is.LiteralOf("new"),
is.LiteralOf("vnew"),
is.LiteralOf("tabnew"),
]);

export * from "./api/types.ts";
export * from "./api/validation.ts";
8 changes: 4 additions & 4 deletions ftplugin/docbase_posts_list.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-open) <cmd>call docbase#buffer_action#posts_list#open(line("."))
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-open-new) <cmd>call docbase#buffer_action#posts_list#open(line("."), "new")
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-open-vnew) <cmd>call docbase#buffer_action#posts_list#open(line("."), "vnew")
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-open-tabedit) <cmd>call docbase#buffer_action#posts_list#open(line("."), "tabedit")
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-open) <cmd>call docbase#buffer_action#posts_list#open(line("."), "")
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-open-new) <cmd>call docbase#buffer_action#posts_list#open(line("."), "horizontal")
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-open-vnew) <cmd>call docbase#buffer_action#posts_list#open(line("."), "vertical")
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-open-tabedit) <cmd>call docbase#buffer_action#posts_list#open(line("."), "tab")
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-prev) <cmd>call docbase#buffer_action#posts_list#prev()
nnoremap <buffer> <plug>(docbase-buffer-action-posts-list-next) <cmd>call docbase#buffer_action#posts_list#next()
8 changes: 4 additions & 4 deletions ftplugin/docbase_teams_list.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
nnoremap <buffer> <plug>(docbase-buffer-action-teams-list-open) <cmd>call docbase#buffer_action#teams_list#open(line("."))
nnoremap <buffer> <plug>(docbase-buffer-action-teams-list-open-new) <cmd>call docbase#buffer_action#teams_list#open(line("."), "new")
nnoremap <buffer> <plug>(docbase-buffer-action-teams-list-open-vnew) <cmd>call docbase#buffer_action#teams_list#open(line("."), "vnew")
nnoremap <buffer> <plug>(docbase-buffer-action-teams-list-open-tabedit) <cmd>call docbase#buffer_action#teams_list#open(line("."), "tabedit")
nnoremap <buffer> <plug>(docbase-buffer-action-teams-list-open) <cmd>call docbase#buffer_action#teams_list#open(line("."), "")
nnoremap <buffer> <plug>(docbase-buffer-action-teams-list-open-new) <cmd>call docbase#buffer_action#teams_list#open(line("."), "horizontal")
nnoremap <buffer> <plug>(docbase-buffer-action-teams-list-open-vnew) <cmd>call docbase#buffer_action#teams_list#open(line("."), "vertical")
nnoremap <buffer> <plug>(docbase-buffer-action-teams-list-open-tabedit) <cmd>call docbase#buffer_action#teams_list#open(line("."), "tab")

0 comments on commit 6417044

Please sign in to comment.