-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
183 lines (152 loc) · 4.48 KB
/
server.ts
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
import {
createConnection,
TextDocuments,
ProposedFeatures,
CompletionItem,
CompletionItemKind,
TextDocumentSyncKind,
InitializeResult,
CodeActionTriggerKind,
} from "vscode-languageserver/node";
import { Position, TextDocument } from "vscode-languageserver-textdocument";
import { findIssuesByTitle, getIssueByKey, getTeamKeys } from "./linear";
import { IssueFragment } from "./types.generated";
type IssuePosition = {
issueKey: string;
positionStart: Position;
positionEnd: Position;
offsetStart: number;
offsetEnd: number;
};
const connection = createConnection(ProposedFeatures.all);
const documents = new TextDocuments(TextDocument);
const teamKeys = new Set<string>();
const issues = new Map<string, IssueFragment | undefined>();
const issuePositions = new Map<string, Array<IssuePosition>>();
connection.onInitialize(async () => {
await getTeamKeys().then((keys) => {
keys.map((k) => {
teamKeys.add(k);
});
});
const result: InitializeResult = {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Incremental,
codeActionProvider: {},
hoverProvider: {},
completionProvider: { resolveProvider: true },
},
};
return result;
});
connection.onCodeAction((params) => {
if (params.context.triggerKind === CodeActionTriggerKind.Invoked) {
const textDocument = documents.get(params.textDocument.uri);
if (!textDocument) {
return;
}
const text = textDocument.getText(params.range);
if (!text) {
return;
}
// TODO: Implement command to create ticket
return [{ title: "Create Ticket" }];
}
});
documents.onDidChangeContent((change) => {
const text = change.document.getText();
const documentPositions: Array<IssuePosition> = [];
Array.from(teamKeys)
.flatMap((prefix) => {
return Array.from(text.matchAll(new RegExp(`${prefix}-[0-9]*`, "g")));
})
.forEach((m) => {
const issueKey = m[0];
const positionStart = change.document.positionAt(m.index ?? 0);
const positionEnd = change.document.positionAt(
issueKey.length + (m.index ?? 0)
);
// Write down that we've seen the issue, but don't
// fetch the definition just yet.
if (!issues.has(issueKey)) {
issues.set(issueKey, undefined);
}
documentPositions.push({
issueKey,
positionStart,
positionEnd,
offsetStart: change.document.offsetAt(positionStart),
offsetEnd: change.document.offsetAt(positionEnd),
});
});
issuePositions.set(change.document.uri, documentPositions);
});
connection.onHover(async (params) => {
const documentPositions = issuePositions.get(params.textDocument.uri);
if (!documentPositions) {
return;
}
const textDocument = documents.get(params.textDocument.uri);
if (!textDocument) {
return;
}
const cursorOffset = textDocument.offsetAt(params.position);
const targetIssue = documentPositions.find(
(dp) => dp.offsetStart <= cursorOffset && dp.offsetEnd > cursorOffset
);
if (!targetIssue) {
return;
}
const issue = await getIssueByKey(targetIssue.issueKey);
if (!issue) {
return;
}
return {
contents: issue.description ?? "Not available",
};
});
connection.onCompletion(async (params): Promise<CompletionItem[]> => {
if (teamKeys.size === 0) {
return [];
}
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}
const lineToPosition = document?.getText({
start: { line: params.position.line, character: 0 },
end: { line: params.position.line, character: params.position.character },
});
const lastMatch = Array.from(teamKeys)
.flatMap((prefix) =>
Array.from(lineToPosition.matchAll(new RegExp(`(${prefix})-(.*)`, "g")))
)
.at(-1);
if (!lastMatch) {
return [];
}
const [, teamKey, searchString] = lastMatch;
if (!teamKey || !searchString) {
return [];
}
const issues = await findIssuesByTitle([teamKey], searchString);
if (issues === undefined) {
return [];
}
return issues.map((issue) => {
let label = `${issue.identifier}: ${issue.title}`;
if (label.length > 60) {
label = label.substring(0, 60) + "...";
}
return {
data: issue,
detail: issue.description ?? "Not available",
insertText: `[${issue.identifier}](${issue.url})`,
filterText: `${issue.team.key}-${issue.title}`,
label,
kind: CompletionItemKind.Reference,
};
});
});
documents.listen(connection);
connection.listen();