opencode-vim 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/bun.lock +15 -0
- package/docs/configuration.md +403 -0
- package/package.json +12 -0
- package/src/modules/snippets/index.tsx +19 -0
- package/src/modules/snippets/loader.ts +147 -0
- package/src/modules/snippets/search.ts +118 -0
- package/src/modules/snippets/skill-loader.ts +81 -0
- package/src/modules/snippets/state.ts +40 -0
- package/src/modules/snippets/trigger.ts +60 -0
- package/src/modules/snippets/types.ts +49 -0
- package/src/modules/snippets/view.tsx +626 -0
- package/src/modules/vim/actions.ts +54 -0
- package/src/modules/vim/config.ts +119 -0
- package/src/modules/vim/index.tsx +96 -0
- package/src/modules/vim/keys.ts +34 -0
- package/src/modules/vim/log.ts +37 -0
- package/src/modules/vim/map.ts +159 -0
- package/src/modules/vim/state.ts +24 -0
- package/src/modules/vim/view.tsx +86 -0
- package/src/modules/vim/vimee.ts +364 -0
- package/src/plugin.tsx +69 -0
- package/src/prompt/host.tsx +38 -0
- package/src/prompt/modules.ts +16 -0
- package/src/prompt/ref.ts +17 -0
- package/src/prompt/root.tsx +56 -0
- package/src/prompt/types.ts +42 -0
- package/tsconfig.json +12 -0
- package/tui.json.example +37 -0
- package/tui.tsx +1 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { SkillInfo, SnippetInfo } from "./types"
|
|
2
|
+
|
|
3
|
+
export type HighlightPart = {
|
|
4
|
+
text: string
|
|
5
|
+
match: boolean
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function normalizeSearchText(input: string) {
|
|
9
|
+
return input.toLowerCase().replace(/[^a-z0-9]/g, "")
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function scoreText(input: string, query: string) {
|
|
13
|
+
const raw = input.toLowerCase()
|
|
14
|
+
const compact = normalizeSearchText(input)
|
|
15
|
+
const needle = query.toLowerCase().trim()
|
|
16
|
+
const compactNeedle = normalizeSearchText(query)
|
|
17
|
+
|
|
18
|
+
if (raw === needle) return 0
|
|
19
|
+
if (compactNeedle && compact === compactNeedle) return 1
|
|
20
|
+
if (raw.startsWith(needle)) return 2
|
|
21
|
+
if (compactNeedle && compact.startsWith(compactNeedle)) return 3
|
|
22
|
+
if (raw.includes(needle)) return 4
|
|
23
|
+
if (compactNeedle && compact.includes(compactNeedle)) return 5
|
|
24
|
+
return Number.POSITIVE_INFINITY
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function snippetDescription(snippet: SnippetInfo) {
|
|
28
|
+
return (snippet.description || snippet.content).replace(/\s+/g, " ").trim()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function scoreSnippet(snippet: SnippetInfo, query: string) {
|
|
32
|
+
if (!query) return 0
|
|
33
|
+
|
|
34
|
+
const nameScore = Math.min(scoreText(snippet.name, query), ...snippet.aliases.map((alias) => scoreText(alias, query)))
|
|
35
|
+
if (Number.isFinite(nameScore)) return nameScore
|
|
36
|
+
|
|
37
|
+
const description = (snippet.description || "").replace(/\s+/g, " ").trim().toLowerCase()
|
|
38
|
+
const lowerQuery = query.toLowerCase()
|
|
39
|
+
if (description.startsWith(lowerQuery)) return 6
|
|
40
|
+
if (description.includes(lowerQuery)) return 7
|
|
41
|
+
return Number.POSITIVE_INFINITY
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function sourceRank(snippet: SnippetInfo) {
|
|
45
|
+
return snippet.source === "project" ? 0 : 1
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function filterSnippets(snippets: SnippetInfo[], query: string) {
|
|
49
|
+
const needle = query.trim()
|
|
50
|
+
return snippets
|
|
51
|
+
.map((snippet) => ({ snippet, score: scoreSnippet(snippet, needle) }))
|
|
52
|
+
.filter((entry) => Number.isFinite(entry.score))
|
|
53
|
+
.sort((left, right) => left.score - right.score || sourceRank(left.snippet) - sourceRank(right.snippet) || left.snippet.name.localeCompare(right.snippet.name))
|
|
54
|
+
.map((entry) => entry.snippet)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function matchedAliases(snippet: SnippetInfo, query: string) {
|
|
58
|
+
const needle = query.trim()
|
|
59
|
+
if (!needle) return []
|
|
60
|
+
return snippet.aliases.filter((alias) => Number.isFinite(scoreText(alias, needle)))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function describeSnippet(snippet: SnippetInfo) {
|
|
64
|
+
return snippetDescription(snippet)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function highlightMatches(input: string, query: string): HighlightPart[] {
|
|
68
|
+
const needle = query.trim().toLowerCase()
|
|
69
|
+
if (!needle) return [{ text: input, match: false }]
|
|
70
|
+
|
|
71
|
+
const lower = input.toLowerCase()
|
|
72
|
+
const parts: HighlightPart[] = []
|
|
73
|
+
let cursor = 0
|
|
74
|
+
|
|
75
|
+
while (cursor < input.length) {
|
|
76
|
+
const index = lower.indexOf(needle, cursor)
|
|
77
|
+
if (index < 0) break
|
|
78
|
+
if (index > cursor) parts.push({ text: input.slice(cursor, index), match: false })
|
|
79
|
+
parts.push({ text: input.slice(index, index + needle.length), match: true })
|
|
80
|
+
cursor = index + needle.length
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (parts.length === 0) return [{ text: input, match: false }]
|
|
84
|
+
if (cursor < input.length) parts.push({ text: input.slice(cursor), match: false })
|
|
85
|
+
return parts
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function skillTag(skill: SkillInfo) {
|
|
89
|
+
return `skill(${skill.name})`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function scoreSkill(skill: SkillInfo, query: string) {
|
|
93
|
+
if (!query) return 0
|
|
94
|
+
const nameScore = Math.min(scoreText(skill.name, query), scoreText(skillTag(skill), query))
|
|
95
|
+
if (Number.isFinite(nameScore)) return nameScore
|
|
96
|
+
const description = (skill.description || "").replace(/\s+/g, " ").trim().toLowerCase()
|
|
97
|
+
const lowerQuery = query.toLowerCase()
|
|
98
|
+
if (description.startsWith(lowerQuery)) return 6
|
|
99
|
+
if (description.includes(lowerQuery)) return 7
|
|
100
|
+
return Number.POSITIVE_INFINITY
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function skillSourceRank(skill: SkillInfo) {
|
|
104
|
+
return skill.source === "project" ? 0 : 1
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function filterSkills(skills: SkillInfo[], query: string) {
|
|
108
|
+
const needle = query.trim()
|
|
109
|
+
return skills
|
|
110
|
+
.map((skill) => ({ skill, score: scoreSkill(skill, needle) }))
|
|
111
|
+
.filter((entry) => Number.isFinite(entry.score))
|
|
112
|
+
.sort((left, right) => left.score - right.score || skillSourceRank(left.skill) - skillSourceRank(right.skill) || left.skill.name.localeCompare(right.skill.name))
|
|
113
|
+
.map((entry) => entry.skill)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function describeSkill(skill: SkillInfo) {
|
|
117
|
+
return (skill.description || skill.content).replace(/\s+/g, " ").trim()
|
|
118
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { dirname, join, parse } from "node:path"
|
|
2
|
+
import type { SkillInfo, SnippetSource } from "./types"
|
|
3
|
+
import { parseFrontmatter } from "./loader"
|
|
4
|
+
|
|
5
|
+
const SKILL_FILE = "SKILL.md"
|
|
6
|
+
|
|
7
|
+
export async function loadSkills(projectDir?: string) {
|
|
8
|
+
const registry = new Map<string, SkillInfo>()
|
|
9
|
+
const home = process.env.HOME
|
|
10
|
+
|
|
11
|
+
if (home) {
|
|
12
|
+
await loadSkillDir(registry, join(home, ".config/opencode/skill"), "global")
|
|
13
|
+
await loadSkillDir(registry, join(home, ".config/opencode/skills"), "global")
|
|
14
|
+
await loadSkillDir(registry, join(home, ".claude/skills"), "global")
|
|
15
|
+
await loadSkillDir(registry, join(home, ".agents/skills"), "global")
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
for (const root of await projectRoots(projectDir)) {
|
|
19
|
+
await loadSkillDir(registry, join(root, ".opencode/skill"), "project")
|
|
20
|
+
await loadSkillDir(registry, join(root, ".opencode/skills"), "project")
|
|
21
|
+
await loadSkillDir(registry, join(root, ".claude/skills"), "project")
|
|
22
|
+
await loadSkillDir(registry, join(root, ".agents/skills"), "project")
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return [...registry.values()].sort((left, right) => sourceRank(left) - sourceRank(right) || left.name.localeCompare(right.name))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function projectRoots(projectDir?: string) {
|
|
29
|
+
if (!projectDir) return []
|
|
30
|
+
const roots: string[] = []
|
|
31
|
+
let current = projectDir
|
|
32
|
+
|
|
33
|
+
while (true) {
|
|
34
|
+
roots.push(current)
|
|
35
|
+
if (await Bun.file(join(current, ".git")).exists()) break
|
|
36
|
+
const parent = dirname(current)
|
|
37
|
+
if (parent === current) break
|
|
38
|
+
current = parent
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return roots.reverse()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function loadSkillDir(registry: Map<string, SkillInfo>, dir: string, source: SnippetSource) {
|
|
45
|
+
let entries: string[]
|
|
46
|
+
try {
|
|
47
|
+
entries = await Array.fromAsync(new Bun.Glob(`*/${SKILL_FILE}`).scan({ cwd: dir, onlyFiles: true }))
|
|
48
|
+
} catch {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (const entry of entries) {
|
|
53
|
+
const skill = await loadSkill(join(dir, entry), source)
|
|
54
|
+
if (skill) registry.set(skill.name.toLowerCase(), skill)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function loadSkill(filePath: string, source: SnippetSource) {
|
|
59
|
+
let raw: string
|
|
60
|
+
try {
|
|
61
|
+
raw = await Bun.file(filePath).text()
|
|
62
|
+
} catch {
|
|
63
|
+
return undefined
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const parsed = parseFrontmatter(raw)
|
|
67
|
+
const folderName = parse(dirname(filePath)).base
|
|
68
|
+
const name = typeof parsed.data.name === "string" && parsed.data.name.trim() ? parsed.data.name.trim() : folderName
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
name,
|
|
72
|
+
content: parsed.content.trim(),
|
|
73
|
+
description: typeof parsed.data.description === "string" ? parsed.data.description : undefined,
|
|
74
|
+
filePath,
|
|
75
|
+
source,
|
|
76
|
+
} satisfies SkillInfo
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function sourceRank(skill: SkillInfo) {
|
|
80
|
+
return skill.source === "project" ? 0 : 1
|
|
81
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { dirname, join } from "node:path"
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises"
|
|
3
|
+
|
|
4
|
+
const SCOPE_GLOBAL = "__global__"
|
|
5
|
+
|
|
6
|
+
export async function addPendingDraft(workspaceDir: string | undefined, name: string) {
|
|
7
|
+
const filePath = statePath("pending-drafts.json")
|
|
8
|
+
const state = await readState(filePath)
|
|
9
|
+
const scope = workspaceDir || SCOPE_GLOBAL
|
|
10
|
+
const current = Array.isArray(state[scope]) ? state[scope].filter((item): item is string => typeof item === "string") : []
|
|
11
|
+
const next = new Set([...current, name.toLowerCase()])
|
|
12
|
+
state[scope] = [...next].sort()
|
|
13
|
+
await writeState(filePath, state)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function markSnippetReloadRequested(workspaceDir?: string) {
|
|
17
|
+
const filePath = statePath("snippet-reload.json")
|
|
18
|
+
const state = await readState(filePath)
|
|
19
|
+
state[workspaceDir || SCOPE_GLOBAL] = Date.now()
|
|
20
|
+
await writeState(filePath, state)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function statePath(name: string) {
|
|
24
|
+
const home = process.env.HOME
|
|
25
|
+
if (!home) throw new Error("HOME is not set")
|
|
26
|
+
return join(home, ".config/opencode/state", name)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function readState(filePath: string) {
|
|
30
|
+
try {
|
|
31
|
+
const parsed = JSON.parse(await Bun.file(filePath).text())
|
|
32
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) return parsed as Record<string, unknown>
|
|
33
|
+
} catch {}
|
|
34
|
+
return {} as Record<string, unknown>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function writeState(filePath: string, state: Record<string, unknown>) {
|
|
38
|
+
await mkdir(dirname(filePath), { recursive: true })
|
|
39
|
+
await writeFile(filePath, `${JSON.stringify(state, null, 2)}\n`)
|
|
40
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { HashtagTriggerMatch, SnippetInfo } from "./types"
|
|
2
|
+
|
|
3
|
+
const HASHTAG_TRIGGER = /(^|\s)#([^\s#]*)$/
|
|
4
|
+
|
|
5
|
+
export function findTrailingHashtagTrigger(input: string): HashtagTriggerMatch | undefined {
|
|
6
|
+
const match = HASHTAG_TRIGGER.exec(input)
|
|
7
|
+
if (!match) return undefined
|
|
8
|
+
|
|
9
|
+
const query = match[2] ?? ""
|
|
10
|
+
const token = `#${query}`
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
start: input.length - token.length,
|
|
14
|
+
end: input.length,
|
|
15
|
+
query,
|
|
16
|
+
token,
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function replaceTrailingHashtag(input: string, name: string) {
|
|
21
|
+
const match = findTrailingHashtagTrigger(input)
|
|
22
|
+
if (!match) return input
|
|
23
|
+
return `${input.slice(0, match.start)}#${name} `
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function insertSnippetTag(input: string, name: string) {
|
|
27
|
+
const match = findTrailingHashtagTrigger(input)
|
|
28
|
+
if (match) return replaceTrailingHashtag(input, name)
|
|
29
|
+
const separator = input.length === 0 || /\s$/.test(input) ? "" : " "
|
|
30
|
+
return `${input}${separator}#${name} `
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function insertSkillLoad(input: string, name: string) {
|
|
34
|
+
const tag = `skill(${name})`
|
|
35
|
+
const match = findTrailingHashtagTrigger(input)
|
|
36
|
+
if (match) return replaceTrailingHashtag(input, tag)
|
|
37
|
+
const separator = input.length === 0 || /\s$/.test(input) ? "" : " "
|
|
38
|
+
return `${input}${separator}#${tag} `
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function preferredSnippetTag(input: string, snippet: SnippetInfo) {
|
|
42
|
+
const query = findTrailingHashtagTrigger(input)?.query
|
|
43
|
+
if (query && snippet.aliases.some((alias) => alias === query)) return query
|
|
44
|
+
return snippet.name
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function insertSnippetTrigger(input: string) {
|
|
48
|
+
if (findTrailingHashtagTrigger(input)) return input
|
|
49
|
+
const separator = input.length === 0 || /\s$/.test(input) ? "" : " "
|
|
50
|
+
return `${input}${separator}#`
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function isReloadCommand(input: string) {
|
|
54
|
+
return input.trim() === "/snippets:reload"
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function stepSelection(current: number, total: number, delta: number) {
|
|
58
|
+
if (total <= 0) return 0
|
|
59
|
+
return Math.max(0, Math.min(total - 1, current + delta))
|
|
60
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export type SnippetSource = "global" | "project"
|
|
2
|
+
|
|
3
|
+
export type SnippetInfo = {
|
|
4
|
+
name: string
|
|
5
|
+
content: string
|
|
6
|
+
aliases: string[]
|
|
7
|
+
description?: string
|
|
8
|
+
filePath: string
|
|
9
|
+
source: SnippetSource
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type SkillInfo = {
|
|
13
|
+
name: string
|
|
14
|
+
content: string
|
|
15
|
+
description?: string
|
|
16
|
+
filePath: string
|
|
17
|
+
source: SnippetSource
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type AutocompleteItem =
|
|
21
|
+
| {
|
|
22
|
+
kind: "snippet"
|
|
23
|
+
id: string
|
|
24
|
+
label: string
|
|
25
|
+
description: string
|
|
26
|
+
aliases: string[]
|
|
27
|
+
snippet: SnippetInfo
|
|
28
|
+
}
|
|
29
|
+
| {
|
|
30
|
+
kind: "skill"
|
|
31
|
+
id: string
|
|
32
|
+
label: string
|
|
33
|
+
description: string
|
|
34
|
+
aliases: string[]
|
|
35
|
+
skill: SkillInfo
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type HashtagTriggerMatch = {
|
|
39
|
+
start: number
|
|
40
|
+
end: number
|
|
41
|
+
query: string
|
|
42
|
+
token: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type SnippetController = {
|
|
46
|
+
accept?: () => boolean
|
|
47
|
+
reload?: () => void
|
|
48
|
+
insertTrigger?: () => void
|
|
49
|
+
}
|