-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathNeovimConfigSyncer.ts
More file actions
161 lines (149 loc) · 5.07 KB
/
NeovimConfigSyncer.ts
File metadata and controls
161 lines (149 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { fetchRepoFileTree } from '$lib/server/github/api';
import type { GithubTree } from '$lib/server/github/schema';
import type { NeovimConfigWithPlugins } from '$lib/server/prisma/neovimconfigs/schema';
import {
syncConfigPlugins,
saveLeaderkey,
syncLanguageServers,
getConfigWithPlugins,
saveLoc
} from '$lib/server/prisma/neovimconfigs/service';
import type { NeovimPluginIdentifier } from '$lib/server/prisma/neovimplugins/schema';
import { getAllNeovimPluginNames } from '$lib/server/prisma/neovimplugins/service';
import { getGithubToken } from '$lib/server/prisma/users/service';
import type { NeovimConfig, User } from '@prisma/client';
import { GithubFileContentTraverser } from './FileContentTraverser';
import { findKnownLanguageServers } from './LanguageServerFinder';
export class NeovimConfigSyncer {
foundPlugins: Record<number, string[]> = {};
treeTraverser: GithubFileContentTraverser;
syncedPluginManager = false;
leaderkey = 'unknown';
loc = 0;
languageServers: string[] = [];
constructor(
token: string,
private tree: GithubTree,
public config: NeovimConfig,
private trackedPlugins: NeovimPluginIdentifier[]
) {
this.treeTraverser = new GithubFileContentTraverser(
token,
config.owner,
config.repo,
tree,
config.root
);
}
async treeSync(): Promise<NeovimConfigWithPlugins> {
return await this.fileSyncer();
}
async locCounter(content: string) {
this.loc = this.loc + content.split('\n').length;
}
async fileSyncer() {
for await (const { content, path } of this.treeTraverser.traverse()) {
this.findPlugins(path, content);
this.syncLeaderKey(content);
this.findLanguageServers(content);
this.locCounter(content);
}
await Promise.all([
saveLeaderkey(this.config.id, this.leaderkey),
saveLoc(this.config.id, this.loc),
syncLanguageServers(this.config.id, this.tree.sha, this.languageServers).then(() => {
const matchedPlugins = Object.entries(this.foundPlugins).map(([id, paths]) => ({
id: Number(id),
paths: paths.join(',')
}));
return syncConfigPlugins(this.config.id, this.tree.sha, matchedPlugins);
})
]);
return getConfigWithPlugins(this.config.id);
}
syncLeaderKey(content: string) {
if (this.leaderkey === 'unknown') {
this.leaderkey = this.findLeaderKey(content) ?? this.leaderkey;
}
}
findLanguageServers(content: string) {
for (const ls of findKnownLanguageServers(content)) {
this.languageServers.push(ls);
}
}
findPlugins(path: string, content: string) {
content = content.replaceAll('\\/', '/'); // dotfyle.json has escaped forwardslashes
const url = `${path}#L{LINENUMBER}`;
for (const plugin of this.trackedPlugins) {
const { owner, name } = plugin;
const fullName = `${owner}/${name}`;
if (content.includes(fullName)) {
for (const [index, line] of content.split('\n').entries())
if (line.includes(fullName)) {
if (!this.foundPlugins[plugin.id]) {
this.foundPlugins[plugin.id] = [];
}
this.foundPlugins[plugin.id].push(url.replace('{LINENUMBER}', String(index + 1)));
}
}
}
}
findLeaderKey(content: string): string | undefined {
for (const line of content.split('\n')) {
if (line.includes('mapleader')) {
const leaderSplit = line.trim().split('=');
if (leaderSplit.length !== 2) continue;
const leaderKey = leaderSplit[1];
const parsedLeaderKey = leaderKey.split('--')[0].trim();
switch (parsedLeaderKey) {
case '"""':
case "'\"'":
return '"';
case '"\'"':
case "'''":
return "'";
case '"-"':
case "'-'":
return '-';
case '";"':
case "';'":
return ';';
case '"\\<Space>"':
case "'\\<Space>'":
case '"\\<space>"':
case "'\\<space>'":
case '" "':
case "' '":
case '" ",':
case "' ',":
return 'Space';
case '","':
case "','":
return ',';
case '"\\"':
case "'\\'":
return '\\';
default:
console.log('Could not match leaderKey', { parsedLeaderKey });
return;
}
}
}
}
}
export async function getNeovimConfigSyncer(
user: User,
config: NeovimConfig
): Promise<NeovimConfigSyncer> {
const token = await getGithubToken(user.id);
const tree = await fetchRepoFileTree(token, config.owner, config.repo, config.branch);
const trackedPlugins = await getAllNeovimPluginNames();
return new NeovimConfigSyncer(token, tree, config, trackedPlugins);
}
export class NeovimConfigSyncerFactory {
constructor(private trackedPlugins: NeovimPluginIdentifier[]) {}
async create(token: string, config: NeovimConfig) {
const tree = await fetchRepoFileTree(token, config.owner, config.repo, config.branch);
return new NeovimConfigSyncer(token, tree, config, this.trackedPlugins);
}
}