Skip to content
New issue

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

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

Already on GitHub? # to your account

Jump after the closing brackets (]]) on autocomplete #998

Merged
merged 5 commits into from
May 30, 2022
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: 8 additions & 0 deletions packages/foam-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@
{
"command": "foam-vscode.open-resource",
"when": "false"
},
{
"command": "foam-vscode.completion-move-cursor",
"when": "false"
}
]
},
Expand Down Expand Up @@ -213,6 +217,10 @@
{
"command": "foam-vscode.create-new-template",
"title": "Foam: Create New Template"
},
{
"command": "foam-vscode.completion-move-cursor",
"title": "Foam: Move cursor after completion"
}
],
"configuration": {
Expand Down
4 changes: 3 additions & 1 deletion packages/foam-vscode/src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import backlinks from './backlinks';
import utilityCommands from './utility-commands';
import hoverProvider from './hover-provider';
import previewNavigation from './preview-navigation';
import completionProvider from './link-completion';
import completionProvider, { completionCursorMove } from './link-completion';
import tagCompletionProvider from './tag-completion';
import linkDecorations from './document-decorator';
import navigationProviders from './navigation-provider';
import wikilinkDiagnostics from './wikilink-diagnostics';
// import completionMoveCursor from './completion-cursor-move';
import refactor from './refactor';
import { FoamFeature } from '../types';

Expand All @@ -43,4 +44,5 @@ export const features: FoamFeature[] = [
previewNavigation,
completionProvider,
tagCompletionProvider,
completionCursorMove,
];
75 changes: 73 additions & 2 deletions packages/foam-vscode/src/features/link-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import { FoamFeature } from '../types';
import { getNoteTooltip, mdDocSelector } from '../utils';
import { fromVsCodeUri, toVsCodeUri } from '../utils/vsc-utils';

export const linkCommitCharacters = ['#', '|'];
export const sectionCommitCharacters = ['|'];

const COMPLETION_CURSOR_MOVE = {
command: 'foam-vscode.completion-move-cursor',
title: 'Foam: Move cursor after completion',
};

export const WIKILINK_REGEX = /\[\[[^[\]]*(?!.*\]\])/;
export const SECTION_REGEX = /\[\[([^[\]]*#(?!.*\]\]))/;

Expand All @@ -31,6 +39,65 @@ const feature: FoamFeature = {
},
};

/**
* always jump to the closing bracket, but jump back the cursor when commit
* by alias divider `|` and section divider `#`
* See https://github.com/foambubble/foam/issues/962,
*/

export const completionCursorMove: FoamFeature = {
activate: (context: vscode.ExtensionContext, foamPromise: Promise<Foam>) => {
context.subscriptions.push(
vscode.commands.registerCommand(
COMPLETION_CURSOR_MOVE.command,
async () => {
const activeEditor = vscode.window.activeTextEditor;
const document = activeEditor.document;
const currentPosition = activeEditor.selection.active;
const cursorChange = vscode.window.onDidChangeTextEditorSelection(
async e => {
const changedPosition = e.selections[0].active;
const preChar = document
.lineAt(changedPosition.line)
.text.charAt(changedPosition.character - 1);

const {
character: selectionChar,
line: selectionLine,
} = e.selections[0].active;

const {
line: completionLine,
character: completionChar,
} = currentPosition;

const inCompleteBySectionDivider =
linkCommitCharacters.includes(preChar) &&
selectionLine === completionLine &&
selectionChar === completionChar + 1;

cursorChange.dispose();
if (inCompleteBySectionDivider) {
await vscode.commands.executeCommand('cursorMove', {
to: 'left',
by: 'character',
value: 2,
});
}
}
);

await vscode.commands.executeCommand('cursorMove', {
to: 'right',
by: 'character',
value: 2,
});
}
)
);
},
};

export class SectionCompletionProvider
implements vscode.CompletionItemProvider<vscode.CompletionItem> {
constructor(private ws: FoamWorkspace) {}
Expand Down Expand Up @@ -70,6 +137,8 @@ export class SectionCompletionProvider
);
item.sortText = String(b.range.start.line).padStart(5, '0');
item.range = replacementRange;
item.commitCharacters = sectionCommitCharacters;
item.command = COMPLETION_CURSOR_MOVE;
return item;
});
return new vscode.CompletionList(items);
Expand Down Expand Up @@ -104,12 +173,12 @@ export class CompletionProvider
// Requires autocomplete only if cursorPrefix matches `[[` that NOT ended by `]]`.
// See https://github.com/foambubble/foam/pull/596#issuecomment-825748205 for details.
const requiresAutocomplete = cursorPrefix.match(WIKILINK_REGEX);

if (!requiresAutocomplete || requiresAutocomplete[0].indexOf('#') >= 0) {
return null;
}

const text = requiresAutocomplete[0];

const replacementRange = new vscode.Range(
position.line,
position.character - (text.length - 2),
Expand All @@ -126,7 +195,8 @@ export class CompletionProvider
item.filterText = resource.uri.getName();
item.insertText = this.ws.getIdentifier(resource.uri);
item.range = replacementRange;
item.commitCharacters = ['#'];
item.command = COMPLETION_CURSOR_MOVE;
item.commitCharacters = linkCommitCharacters;
return item;
});
const placeholders = Array.from(this.graph.placeholders.values()).map(
Expand All @@ -136,6 +206,7 @@ export class CompletionProvider
vscode.CompletionItemKind.Interface
);
item.insertText = uri.path;
item.command = COMPLETION_CURSOR_MOVE;
item.range = replacementRange;
return item;
}
Expand Down