-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtacticsuggestions.ts
178 lines (157 loc) · 7.34 KB
/
tacticsuggestions.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
import { Message } from 'lean-client-js-node';
import { CodeActionContext, CodeActionProvider, Command, commands,
DiagnosticSeverity, Disposable, DocumentSelector, languages,
Position, Range, Selection, TextDocument, TextEditor, Uri, window } from 'vscode';
import { InfoProvider } from './infoview';
import { Server } from './server';
import { regexGM, magicWord, regexM } from './trythis';
import { CodePointPosition } from './utils/utf16Position';
/** Pastes suggestions provided by tactics such as `squeeze_simp` */
export class TacticSuggestions implements Disposable, CodeActionProvider {
private subscriptions: Disposable[] = [];
constructor(private server: Server, infoView: InfoProvider, private leanDocs: DocumentSelector) {
const commandHandler = async (textEditor: TextEditor) => {
const msg = this.findSelectedMessage(textEditor);
if (msg === null) return;
await this.pasteIntoEditor(msg, textEditor, null);
};
const infoViewCommandHandler = async (m: Message, suggestion: string) => {
const textEditor = this.findTextEditor(m.file_name);
await this.pasteIntoEditor(m, textEditor, suggestion);
// Focus text editor
await window.showTextDocument(textEditor.document, {viewColumn:textEditor.viewColumn});
};
this.subscriptions.push(
commands.registerTextEditorCommand('lean.pasteTacticSuggestion', commandHandler),
commands.registerCommand('_lean.pasteTacticSuggestion', infoViewCommandHandler),
languages.registerCodeActionsProvider(this.leanDocs, this),
);
}
dispose(): void {
for (const s of this.subscriptions) { s.dispose(); }
}
private findTextEditor(fileName: string) {
for (const textEditor of window.visibleTextEditors) {
// note we cannot compare URIs as the `Message` type doesn't contain them
if (textEditor.document.fileName === fileName) {
return textEditor;
}
}
}
private findSelectedMessage(textEditor: TextEditor) {
const curFileName = textEditor.document.fileName;
const curPosition = CodePointPosition.ofPosition(textEditor.document, textEditor.selection.active);
// Find message closest to the cursor
const messages = this.server.messages
.filter((m: Message) => m.file_name === curFileName &&
m.pos_line === curPosition.line + 1 &&
m.pos_col <= curPosition.character)
.sort((a, b) => b.pos_col - a.pos_col);
if (messages.length === 0) return null;
return messages[0];
}
private async pasteIntoEditor(m: Message, textEditor: TextEditor, suggestion: string | null) {
if (suggestion === null) {
// Find first suggestion in message
const suggs = regexM.exec(m.text);
if (!suggs) return;
suggestion = suggs[1];
}
// remove leading and trailing whitespace
suggestion = suggestion.trim();
// Start of the tactic call to replace
const startPos = new CodePointPosition(m.pos_line - 1, m.pos_col).toPosition(textEditor.document);
const startLine = startPos.line;
let startCol = startPos.character;
// Try to determine the end of the tactic call to replace.
// Heuristic: Find the next comma, semicolon, unmatched closing bracket,
// or newline that is not enclosed in brackets. To keep things simple,
// we use only one counter for all kinds of brackets.
let openBrackets = 0;
let endLine: number;
let endCol: number;
lineLoop:
for (endLine = startLine; endLine < textEditor.document.lineCount; endLine++) {
const chars = textEditor.document.lineAt(endLine).text.split('').entries();
// Iterate over every character of the line
for (const [col, char] of chars) {
// Only search from where the tactic starts
if (endLine > startLine || col > startCol) {
if (openBrackets === 0 && [',',';'].includes(char)) {
endCol = col;
break lineLoop;
}
if (['(','[','{','⟨','⦃'].includes(char)) {
openBrackets++;
}
if (['⦄','⟩','}',']',')'].includes(char)) {
if (openBrackets === 0) {
endCol = col;
break lineLoop;
} else {
openBrackets--;
}
}
}
}
if (openBrackets === 0) {
endCol = textEditor.document.lineAt(endLine).range.end.character;
break lineLoop;
}
}
// Jump to the end of the tactic call
const lastPos = new Position(endLine, endCol)
textEditor.selection = new Selection(lastPos, lastPos)
// Now check for `exact ` at the beginning of the suggestion
// and `by ` before the tactic call (or `begin ` before, and `, end` after)
// and if both occur, remove them.
if (suggestion.startsWith('exact ')) {
const pre = textEditor.document.lineAt(startLine).text.substring(0, startCol);
// First check for `by `:
if (pre.endsWith('by ')) {
startCol = startCol - 3;
suggestion = suggestion.substring(6);
// Then check for `begin ... end`:
} else if (pre.endsWith('begin ')) {
const post = textEditor.document.lineAt(endLine).text.substring(endCol);
const matches = post.match(/^,?\s*end/g);
if (matches) {
startCol = startCol - 6;
endCol = endCol + matches[0].length;
suggestion = suggestion.substring(6);
}
}
}
// Replace tactic call by suggestion
const range = new Range(
new Position(startLine, startCol),
new Position(endLine, endCol)
)
await textEditor.edit(editBuilder => {
editBuilder.replace(range, suggestion)
});
// Strangely, the cursor moves during the edit, but the selection anchor
// does not. Therefore, move the anchor to the cursor:
textEditor.selection =
new Selection(textEditor.selection.active, textEditor.selection.active);
}
provideCodeActions(document: TextDocument, range: Range, context: CodeActionContext): Command[] {
const cmds: Command[] = [];
// filter diagnostic messages
for (const diag of context.diagnostics) {
if (diag.severity !== DiagnosticSeverity.Information) { continue; }
// identify message
const msg = this.server.messages.find((m) => m.file_name === document.fileName &&
m.text === diag.message);
// each "Try this" becomes a code action
for (const [, tactic] of diag.message.matchAll(regexGM)) {
cmds.push({
title: tactic,
command: '_lean.pasteTacticSuggestion',
arguments: [msg, tactic]
});
}
}
return cmds;
}
}