Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add widget effect support for specifying location of text to insert. #234

Merged
merged 2 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions infoview/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class WidgetErrorBoundary extends React.Component<{children: any},{error?: {mess

/** [todo] pending adding to lean-client-js */
export type WidgetEffect =
| {kind: 'insert_text'; text: string}
| {kind: 'insert_text'; text: string, line?: number; line_type?: 'relative' | 'absolute'}
| {kind: 'reveal_position'; file_name: string; line: number; column: number}
| {kind: 'highlight_position'; file_name: string; line: number; column: number}
| {kind: 'clear_highlighting'}
Expand All @@ -82,7 +82,19 @@ export type WidgetEffect =
function applyWidgetEffect(widget: WidgetIdentifier, file_name: string, effect: WidgetEffect) {
switch (effect.kind) {
case 'insert_text':
const loc = {file_name, line: widget.line, column: widget.column};
let line = widget.line;
if (effect.line_type && effect.line) {
if (effect.line_type === 'relative') {
line = widget.line + effect.line;
} else {
line = effect.line
}
}
const loc = {
file_name,
line,
column: widget.column
};
edit(loc, effect.text);
break;
case 'reveal_position': reveal({file_name: effect.file_name || file_name, line: effect.line, column: effect.column}); break;
Expand Down
12 changes: 4 additions & 8 deletions src/infoview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class InfoProvider implements Disposable {
this.proxyConnection.send(JSON.parse(message.payload));
}
private async handleInsertText(message: InsertTextMessage) {
const new_command = message.text;
let new_command = message.text;
let editor: TextEditor = null;
if (message.loc) {
editor = window.visibleTextEditors.find(e => e.document.fileName === message.loc.file_name);
Expand All @@ -232,14 +232,10 @@ export class InfoProvider implements Disposable {
const prev_line = editor.document.lineAt(pos.line - 1);
const spaces = prev_line.firstNonWhitespaceCharacterIndex;
const margin_str = [...Array(spaces).keys()].map(x => ' ').join('');

// [hack] for now, we assume that there is only ever one command per line
// and that the command should be inserted on the line above this one.

new_command = new_command.replace(/\n/g, '\n' + margin_str);
new_command = `\n${margin_str}${new_command}`;
await editor.edit((builder) => {
builder.insert(
prev_line.range.end,
`\n${margin_str}${new_command}`);
builder.insert(prev_line.range.end, new_command);
EdAyers marked this conversation as resolved.
Show resolved Hide resolved
});
editor.selection = new Selection(pos.line, spaces, pos.line, spaces);
}
Expand Down