Skip to content

Commit

Permalink
Merge pull request #671 from ejgallego/move_backwards_forward
Browse files Browse the repository at this point in the history
[vscode] Add simple commands to move backwards / forward in a proof.
  • Loading branch information
ejgallego authored Apr 5, 2024
2 parents d0ee3b0 + c233b26 commit cc7dd9a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
#513)
- allow more than one input position in `selectionRange` LSP call
(@ejgallego, #667, fixes #663)
- new VSCode commands to allow to move one sentence backwards /
forward, this is particularly useful when combined with lazy
checking mode (@ejgallego, #671, fixes #263, fixes #580)

# coq-lsp 0.1.8.1: Spring fix
-----------------------------
Expand Down
8 changes: 8 additions & 0 deletions editor/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@
{
"command": "coq-lsp.trim",
"title": "Coq LSP: Request the server to trim caches and compact memory (useful to try reduce memory comsumption)"
},
{
"command": "coq-lsp.sentenceNext",
"title": "Coq LSP: try to jump to next Coq sentence"
},
{
"command": "coq-lsp.sentenceBack",
"title": "Coq LSP: try to jump to previous Coq sentence"
}
],
"keybindings": [
Expand Down
5 changes: 4 additions & 1 deletion editor/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ThemeColor,
WorkspaceConfiguration,
Disposable,
DocumentSelector,
languages,
} from "vscode";

Expand Down Expand Up @@ -40,6 +39,7 @@ import {
import { InfoPanel, goalReq } from "./goals";
import { FileProgressManager } from "./progress";
import { coqPerfData, PerfDataView } from "./perf";
import { sentenceNext, sentenceBack } from "./edit";

let config: CoqLspClientConfig;
let client: BaseLanguageClient;
Expand Down Expand Up @@ -338,6 +338,9 @@ export function activateCoqLSP(
coqEditorCommand("document", getDocument);
coqEditorCommand("save", saveDocument);

coqEditorCommand("sentenceNext", sentenceNext);
coqEditorCommand("sentenceBack", sentenceBack);

createEnableButton();

start();
Expand Down
45 changes: 45 additions & 0 deletions editor/code/src/edit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Edition facilities for Coq files
import { TextEditor, Position, Range, Selection } from "vscode";

export function sentenceBack(editor: TextEditor) {
// Slice from the beginning of the document
let cursor = editor.selection.active;
let range = new Range(editor.document.positionAt(0), cursor);
let text = editor.document.getText(range);

// what a hack
let regres = text.match(/\.\s+/g);
if (regres) {
let match = regres.at(-2) || "";
var index = 0;
if (match == regres.at(-1) || "") {
let idx = text.lastIndexOf(match);
index = text.lastIndexOf(match, idx - 1) + match.length;
} else {
index = text.lastIndexOf(match) + match.length;
}
let newCursor = editor.document.positionAt(index);
editor.selection = new Selection(newCursor, newCursor);
}
}

export function sentenceNext(editor: TextEditor) {
// Slice to the end of the document
let cursor = editor.selection.active;
let lastLine = editor.document.lineCount - 1;
let lastPos = editor.document.lineAt(lastLine).range.end;
let range = new Range(cursor, lastPos);
let text = editor.document.getText(range);

// get the offset of the first match
let regres = text.match(/\.\s+/);
if (regres) {
regres;
let match: any = regres[0];
let index = regres.index + match.length;
let newCursor = editor.document.positionAt(
editor.document.offsetAt(cursor) + index
);
editor.selection = new Selection(newCursor, newCursor);
}
}

0 comments on commit cc7dd9a

Please sign in to comment.