-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcompletions.ts
More file actions
199 lines (189 loc) · 5.37 KB
/
completions.ts
File metadata and controls
199 lines (189 loc) · 5.37 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/**
* Utility functions related to completions.
*
* @module
*/
import {
type Datastore,
entityToObject,
objectSetKey,
objectToEntity,
} from "google_datastore";
import { getDatastore } from "./auth.ts";
import { lookup } from "./cache.ts";
import { kinds } from "./consts.ts";
import { enqueue } from "./process.ts";
import type {
CompletionItems,
ModuleEntry,
PathCompletion,
PathCompletions,
} from "./types.d.ts";
const completionCache = new Map<string, PathCompletions>();
function isImportable(path: string) {
return /\.(ts|tsx|mts|cts|js|jsx|mjs|cjs|json)$/i.test(path);
}
function isHidden(path: string) {
return /\/\./.test(path);
}
function hasModules(
map: Map<string, PathCompletion>,
entry: PathCompletion,
): boolean {
if (entry.modules.length) {
return true;
}
if (entry.dirs) {
for (const dir of entry.dirs) {
const e = map.get(dir);
if (e) {
return hasModules(map, e);
}
}
}
return false;
}
function toCompletions(
module: string,
version: string,
entries: ModuleEntry[],
): PathCompletions {
const completionMap = new Map<string, PathCompletion>();
for (const entry of entries) {
if (isHidden(entry.path)) {
continue;
}
if (entry.type === "dir") {
let path = entry.path;
if (path !== "/") {
path = `${path}/`;
}
if (!completionMap.has(path)) {
completionMap.set(path, {
path,
default: entry.default,
dirs: entry.dirs?.map((p) => `${p}/`),
modules: [],
});
}
} else {
if (isImportable(entry.path)) {
const parts = entry.path.split("/");
parts.pop();
const parent = `${parts.join("/")}/`;
const completionItem = completionMap.get(parent);
if (!completionItem) {
console.error("Missing parent:", parent);
continue;
}
completionItem.modules.push({ path: entry.path });
}
}
}
const items: PathCompletion[] = [];
for (const value of completionMap.values()) {
if (hasModules(completionMap, value)) {
items.push(value);
}
}
return { name: module, version, items };
}
let datastore: Datastore | undefined;
export function getCompletionItems(
completions: PathCompletions,
path: string,
): CompletionItems | undefined {
const parts = path.split("/");
const last = parts.pop();
const dir = last ? `${parts.join("/")}/` : path;
const pathCompletion = completions.items.find(({ path }) => path === dir);
if (pathCompletion) {
const items: string[] = [];
let hasDir = false;
if (pathCompletion.dirs) {
for (const dir of pathCompletion.dirs) {
if (dir.startsWith(path)) {
hasDir = true;
items.push(dir);
}
}
}
for (const { path: mod } of pathCompletion.modules) {
if (mod.startsWith(path)) {
items.push(mod);
}
}
// when the client queries a sub path, it will omit the trailing `/`, here
// we check if that if the path with a trailing `/` is the only possible
// result, we will return that instead.
if (items.length === 1 && items.includes(`${path}/`)) {
return getCompletionItems(completions, `${path}/`);
}
let preselect: string | undefined;
if (pathCompletion.default && items.includes(pathCompletion.default)) {
preselect = pathCompletion.default.slice(1);
}
return {
// we need to strip the leading `/` from the completions as the client
// isn't expecting them.
items: items.map((path) => path.slice(1)),
isIncomplete: hasDir,
preselect,
};
}
}
/** Resolve with a collection of directories and paths for a module and version
* for building completion items. */
export async function getCompletions(
module: string,
version: string,
): Promise<PathCompletions | undefined> {
if (version === "__latest__") {
const [moduleItem] = await lookup(module);
if (!moduleItem || !moduleItem.latest_version) {
return;
}
version = moduleItem.latest_version;
}
const key = `${module}@${version}`;
let pathCompletions = completionCache.get(key);
if (!pathCompletions) {
datastore = datastore ?? await getDatastore();
const res = await datastore.lookup(datastore.key(
[kinds.MODULE_KIND, module],
[kinds.PATH_COMPLETIONS_KIND, version],
));
if (res.found && res.found.length === 1) {
const [{ entity }] = res.found;
pathCompletions = entityToObject(entity);
completionCache.set(key, pathCompletions);
}
if (!pathCompletions) {
const entries = await datastore.query<ModuleEntry>(
datastore.createQuery(kinds.MODULE_ENTRY_KIND).hasAncestor(
datastore.key(
[kinds.MODULE_KIND, module],
[kinds.MODULE_VERSION_KIND, version],
),
),
);
if (entries.length) {
pathCompletions = toCompletions(module, version, entries);
objectSetKey(
pathCompletions,
datastore.key(
[kinds.MODULE_KIND, module],
[kinds.PATH_COMPLETIONS_KIND, version],
),
);
completionCache.set(key, pathCompletions);
enqueue({
kind: "commitMutations",
mutations: [{ upsert: objectToEntity(pathCompletions) }],
});
}
}
}
return pathCompletions;
}