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 all commits
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
8 changes: 4 additions & 4 deletions infoview/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Server, Transport, Connection, Event, TransportError, Message } from 'lean-client-js-core';
import { ToInfoviewMessage, FromInfoviewMessage, Config, Location, defaultConfig, PinnedLocation } from '../src/shared';
import { ToInfoviewMessage, FromInfoviewMessage, Config, Location, defaultConfig, PinnedLocation, InsertTextMessage } from '../src/shared';
declare const acquireVsCodeApi;
const vscode = acquireVsCodeApi();

Expand All @@ -10,15 +10,15 @@ export function post(message: FromInfoviewMessage): void { // send a message to
export function clearHighlight(): void { return post({ command: 'stop_hover'}); }
export function highlightPosition(loc: Location): void { return post({ command: 'hover_position', loc}); }
export function copyToComment(text: string): void {
post({ command: 'insert_text', text: `/-\n${text}\n-/\n`});
post({ command: 'insert_text', text: `/-\n${text}\n-/\n`, insert_type: 'relative'});
}

export function reveal(loc: Location): void {
post({ command: 'reveal', loc });
}

export function edit(loc: Location, text: string): void {
post({ command: 'insert_text', loc, text });
export function edit(loc: Location, text: string, insert_type : InsertTextMessage['insert_type'] = 'relative'): void {
post({ command: 'insert_text', loc, text, insert_type });
}

export function copyText(text: string): void {
Expand Down
13 changes: 10 additions & 3 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; column?: number; file_name?: string; insert_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,8 +82,15 @@ 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};
edit(loc, effect.text);
const insert_type = effect.insert_type ?? 'relative';
if (insert_type === 'relative') {
const line = widget.line + (effect.line ?? 0);
edit({file_name, line, column:0}, effect.text, 'relative');
} else if (insert_type === 'absolute') {
edit({file_name:effect.file_name ?? file_name, line: effect.line, column: effect.column}, effect.text, 'absolute')
} else {
throw new Error(`unrecognised effect insert type ${insert_type}`);
}
break;
case 'reveal_position': reveal({file_name: effect.file_name || file_name, line: effect.line, column: effect.column}); break;
case 'highlight_position': highlightPosition({file_name: effect.file_name || file_name, line: effect.line, column: effect.column}); break;
Expand Down
37 changes: 22 additions & 15 deletions src/infoview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ export class InfoProvider implements Disposable {
this.proxyConnection.send(JSON.parse(message.payload));
}
private async handleInsertText(message: InsertTextMessage) {
const 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 @@ -227,25 +226,33 @@ export class InfoProvider implements Disposable {
}
if (!editor) {return; }
const pos = message.loc ? this.positionOfLocation(message.loc) : editor.selection.active;
const current_selection_range = editor.selection;
const cursor_pos = current_selection_range.active;
const prev_line = editor.document.lineAt(pos.line - 1);
const spaces = prev_line.firstNonWhitespaceCharacterIndex;
const margin_str = [...Array(spaces).keys()].map(x => ' ').join('');
const insert_type = message.insert_type ?? 'relative';
if (insert_type === 'relative') {
// in this case, assume that we actually want to insert at the same
// indentation level as the neighboring text
const current_selection_range = editor.selection;
const cursor_pos = current_selection_range.active;
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.
let new_command = message.text.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}`);
});
editor.selection = new Selection(pos.line, spaces, pos.line, spaces);
await editor.edit((builder) => {
builder.insert(prev_line.range.end, new_command);
});
editor.selection = new Selection(pos.line, spaces, pos.line, spaces);
} else {
await editor.edit((builder) => {
builder.insert(pos, message.text);
});
editor.selection = new Selection(pos, pos)
}
}

private positionOfLocation(l: Location): Position {
return new Position(l.line - 1, l.column);
return new Position(l.line - 1, l.column ?? 0);
}
private makeLocation(file_name: string, pos: Position): Location {
return {
Expand Down
1 change: 1 addition & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface InsertTextMessage {
/** If no location is given set to be the cursor position. */
loc?: Location;
text: string;
insert_type: 'absolute' | 'relative';
}
export interface RevealMessage {
command: 'reveal';
Expand Down