From 989b3b9d8c0d4f38a65bc61ba2414b514d11e89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sch=C3=B6dler?= Date: Sat, 6 May 2023 15:34:38 +0200 Subject: [PATCH] Adds go to source feature --- DEV.md | 5 +++-- src/controllers/viewController.ts | 17 +++++++++++++++-- src/services/loggingService.ts | 12 +++++++++++- src/settings.ts | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/DEV.md b/DEV.md index 6979d26..c9a326b 100644 --- a/DEV.md +++ b/DEV.md @@ -3,13 +3,14 @@ ## Roadmap - [x] General UX Improvement -- [ ] Add "Goto Source" action on suggestions +- [x] Add "Goto Source" action on suggestions - [x] Fix occurrences hint not being updated when new occurrences are found ## Refactors - [x] Refactor handling / updating of `TreeItem`s. Use a reactive approach instead of manually updating the tree. -- [ ] Refactor `Editor.getWordLookup()` there has to be a better way of retrieving plain-text from an `Editor` (Without manually removing MD syntax) +- [ ] Refactor `Editor.getWordLookup()` there has to be a better way of retrieving plain-text from an `Editor` (Without manually removing MD syntax) (Maybe a code mirror plugin?) +- [ ] Minimally stem words before matching them to cache items ## Releasing new releases diff --git a/src/controllers/viewController.ts b/src/controllers/viewController.ts index d266aec..de7adfb 100644 --- a/src/controllers/viewController.ts +++ b/src/controllers/viewController.ts @@ -10,9 +10,10 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -import { ButtonComponent, Editor, EditorPosition } from 'obsidian'; +import { ButtonComponent, Editor, EditorPosition, MarkdownView } from 'obsidian'; import { Match, Occurrence, Suggestion } from 'src/model/suggestion'; import { CacheMatch } from 'src/services/indexingService'; +import { CrossbowLoggingService } from 'src/services/loggingService'; import { CrossbowSettingsService } from 'src/services/settingsService'; import { TreeItem, TreeItemButtonIcon, TreeItemLeaf } from 'src/view/treeItem'; import { CrossbowView } from 'src/view/view'; @@ -153,7 +154,19 @@ export class CrossbowViewController { // Go to source action matchTreeItem.addButton('Go To Source', TreeItemButtonIcon.Search, () => { - console.warn("🏹: 'Go To Source' is not yet implemented"); + const leaf = app.workspace.getLeaf(true); + app.workspace.setActiveLeaf(leaf); + leaf.openFile(match.cacheMatch.file).then(() => { + if (leaf.view instanceof MarkdownView) { + if (match.cacheMatch.item?.position) { + const { line, col } = match.cacheMatch.item.position.start + leaf.view.editor.setCursor(line, col); + } + } + else { + CrossbowLoggingService.forceLog('warn', 'Could not go to source, not a markdown file'); + } + }); }); matchTreeItem.addTextSuffix(match.cacheMatch.type); diff --git a/src/services/loggingService.ts b/src/services/loggingService.ts index 1921851..82d512a 100644 --- a/src/services/loggingService.ts +++ b/src/services/loggingService.ts @@ -15,7 +15,17 @@ import { CrossbowSettingsService } from './settingsService'; export class CrossbowLoggingService { public constructor(private readonly settingsService: CrossbowSettingsService) {} + private static LOGGER_PREFIX = '🏹: '; + public debugLog(message: string): void { - this.settingsService.getSettings().useLogging && console.log(`🏹: ${message}`); + this.settingsService.getSettings().useLogging && console.log(CrossbowLoggingService.LOGGER_PREFIX + message); + } + + public debugWarn(message: string): void { + this.settingsService.getSettings().useLogging && console.warn(CrossbowLoggingService.LOGGER_PREFIX + message); + } + + public static forceLog(type: 'warn' | 'log', message: string): void { + console[type](CrossbowLoggingService.LOGGER_PREFIX + message); } } diff --git a/src/settings.ts b/src/settings.ts index f448447..717f930 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -10,7 +10,7 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -import { App, PluginSettingTab, Setting } from 'obsidian'; +import { App, ButtonComponent, PluginSettingTab, Setting } from 'obsidian'; import CrossbowPlugin from './main'; import { CrossbowPluginSettings, CrossbowSettingsService } from './services/settingsService';