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

Wip/add comment string when clauses #1030

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changes to Calva.

## [Unreleased]
- Fix: [Allow user to override keybindings in comments and/or strings](https://github.com/BetterThanTomorrow/calva/issues/1023)
PEZ marked this conversation as resolved.
Show resolved Hide resolved

## [2.0.171] - 2021-02-10
- Update clojure-lsp to version 2021.02.09-18.28.06 (Fix: [Auto completion does not work in clojure-lsp only mode (no repl connection)](https://github.com/BetterThanTomorrow/calva/issues/996#issuecomment-776148282))
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const config = {
REPL_FILE_EXT: 'calva-repl',
KEYBINDINGS_ENABLED_CONFIG_KEY: 'calva.keybindingsEnabled',
KEYBINDINGS_ENABLED_CONTEXT_KEY: 'calva:keybindingsEnabled'
KEYBINDINGS_ENABLED_CONTEXT_KEY: 'calva:keybindingsEnabled',
CURSOR_CONTEXT_IN_STRING: 'calva:cursorInString',
CURSOR_CONTEXT_IN_COMMENT: 'calva:cursorInComment',
};

type ReplSessionType = 'clj' | 'cljs';
Expand Down
8 changes: 8 additions & 0 deletions src/cursor-doc/token-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,14 @@ export class LispTokenCursor extends TokenCursor {
return false;
}

/**
* Indicates if the current token is in a comment line
*/
withinComment() {
const cursor = this.clone();
return cursor.getToken().type === 'comment' || cursor.getPrevToken().type === 'comment';
}

/**
* Tells if the cursor is inside a properly closed list.
*/
Expand Down
55 changes: 55 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import config from './config';
import handleNewCljFiles from './fileHandler';
import lsp from './lsp';
import * as snippets from './custom-snippets';
import * as docMirror from './doc-mirror'

async function onDidSave(document) {
let {
Expand Down Expand Up @@ -219,6 +220,7 @@ async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor((editor) => {
status.update();
replHistory.setReplHistoryCommandsActiveContext(editor);
setCursorContextIfChanged(editor);
}));
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(annotations.onDidChangeTextDocument));
context.subscriptions.push(new vscode.Disposable(() => {
Expand All @@ -231,6 +233,7 @@ async function activate(context: vscode.ExtensionContext) {
}));
context.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(event => {
replHistory.setReplHistoryCommandsActiveContext(event.textEditor);
setCursorContextIfChanged(event.textEditor);
}));
context.subscriptions.push(vscode.workspace.onDidCloseTextDocument(document => {
if (outputWindow.isResultsDoc(document)) {
Expand Down Expand Up @@ -311,6 +314,58 @@ async function activate(context: vscode.ExtensionContext) {
}
}

type CursorContext = 'calva-standard' | AltCursorContext;
type AltCursorContext = 'string' | 'comment';
let lastContext: CursorContext = null;

function setCursorContextIfChanged(editor: vscode.TextEditor) {
if (!editor || !editor.document || editor.document.languageId !== 'clojure') return;

const currentContext = determineCursorContext(editor.document, editor.selection.active);
setCursorContext(currentContext);
}

function setCursorContext(currentContext: CursorContext) {
if (lastContext === currentContext) {
return;
}
lastContext = currentContext;
const commentContext = config.CURSOR_CONTEXT_IN_COMMENT;
const stringContext = config.CURSOR_CONTEXT_IN_STRING;
vscode.commands.executeCommand('setContext', commentContext, false);
vscode.commands.executeCommand('setContext', stringContext, false);

switch (currentContext) {
case 'calva-standard':
break;
case 'comment':
vscode.commands.executeCommand('setContext', commentContext, true);
break;
case 'string':
vscode.commands.executeCommand('setContext', stringContext, true);
break;
default:
// TODO: throw? log?
}
}

function determineCursorContext(document: vscode.TextDocument, position: vscode.Position): CursorContext {
const idx = document.offsetAt(position);
const mirrorDoc = docMirror.getDocument(document);
const tokenCursor = mirrorDoc.getTokenCursor(idx);

let context: CursorContext;
if (tokenCursor.withinString()) {
context = 'string';
} else if (tokenCursor.withinComment()) {
context = 'comment';
} else {
context = 'calva-standard';
}

return context;
}

function deactivate(): Promise<void> | undefined {
state.analytics().logEvent("LifeCycle", "Deactivated").send();
jackIn.calvaJackout();
Expand Down