From 9ce62a6b6d1bf619eb1cd9dff33c5bcb1d5b83d5 Mon Sep 17 00:00:00 2001 From: ez4gis Date: Sun, 17 Jan 2021 16:45:10 -0800 Subject: [PATCH] feat: Add memo:refFocusedOrHovered --- package.json | 4 ++-- src/features/referenceContextWatcher.ts | 6 ++++-- src/utils/utils.ts | 11 ++++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b12ebc81..9be8a1e2 100644 --- a/package.json +++ b/package.json @@ -157,7 +157,7 @@ "command": "memo.openReferenceBeside", "key": "ctrl+shift+enter", "mac": "cmd+shift+enter", - "when": "editorTextFocus && editorLangId == markdown" + "when": "editorTextFocus && editorLangId == markdown && memo:refFocusedOrHovered" } ], "menus": { @@ -175,7 +175,7 @@ }, { "command": "memo.openReferenceBeside", - "when": "editorLangId == markdown" + "when": "editorLangId == markdown && memo:refFocusedOrHovered" } ] }, diff --git a/src/features/referenceContextWatcher.ts b/src/features/referenceContextWatcher.ts index ed77b96b..88c054af 100644 --- a/src/features/referenceContextWatcher.ts +++ b/src/features/referenceContextWatcher.ts @@ -1,9 +1,11 @@ import { window, workspace, commands, ExtensionContext } from 'vscode'; -import { getRefUriUnderCursor } from '../utils'; +import { getRefUriUnderCursor, getRefUnderCursor, getReferenceAtPosition } from '../utils'; -const updateRefExistsContext = () => +const updateRefExistsContext = () => { commands.executeCommand('setContext', 'memo:refUnderCursorExists', !!getRefUriUnderCursor()); + commands.executeCommand('setContext', 'memo:refFocusedOrHovered', !!getRefUnderCursor()); +}; export const activate = (context: ExtensionContext) => { context.subscriptions.push( diff --git a/src/utils/utils.ts b/src/utils/utils.ts index b87e5ba1..0b945307 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -432,13 +432,22 @@ export const ensureDirectoryExists = (filePath: string) => { } }; -export const getRefUriUnderCursor = (): vscode.Uri | null | undefined => { +export const getRefUnderCursor = (): + | { range: vscode.Range; ref: string; label: string } + | null + | undefined => { const activeTextEditor = vscode.window.activeTextEditor; const refAtPos = activeTextEditor && getReferenceAtPosition(activeTextEditor.document, activeTextEditor.selection.start); + return refAtPos; +}; + +export const getRefUriUnderCursor = (): vscode.Uri | null | undefined => { + const refAtPos = getRefUnderCursor(); + return refAtPos && findUriByRef(getWorkspaceCache().allUris, refAtPos.ref); };