Skip to content

Commit

Permalink
support both relativePath and uniqueFilename Peek Definitions. Add im…
Browse files Browse the repository at this point in the history
…age. Add packaging scripts
  • Loading branch information
kortina committed Feb 18, 2020
1 parent f16bd10 commit c975a0a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
Binary file added images/vscode-markdown-notes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/vscode-markdown-notes.psd
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-markdown-notes",
"displayName": "VS Code Markdown Notes",
"description": "______________ ________________",
"description": "Support [[double bracket]] wiki links (like Bear, Roam, etc) with keyboard navigation and Peek Definition previews of linked notes.",
"version": "0.0.1",
"publisher": "kortina",
"repository": {
Expand Down Expand Up @@ -54,12 +54,14 @@
}
}
},
"icon": "icon.png",
"icon": "images/vscode-markdown-notes.png",
"activationEvents": [
"*"
],
"main": "./out/extension.js",
"scripts": {
"vpackage": "./node_modules/.bin/vsce package",
"vpublish": "./node_modules/.bin/vsce publish",
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"lint": "tslint -p ./",
Expand Down
47 changes: 35 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import { basename, dirname, join, normalize, relative } from 'path';
import { basename, dirname, join, normalize, relative, resolve } from 'path';
import { existsSync } from 'fs';
import {
CompletionItemProvider,
TextDocument,
Expand Down Expand Up @@ -47,6 +48,7 @@ class MarkdownFileCompletionItemProvider implements CompletionItemProvider {
let t = document.getText(line);
console.log('line:', t);
let files = (await workspace.findFiles('**/*')).filter(
// TODO: paramaterize extensions
f => f.scheme == 'file' && f.path.match(/\.(md|markdown)/i)
);
let items = files.map(f => {
Expand All @@ -61,27 +63,48 @@ class MarkdownFileCompletionItemProvider implements CompletionItemProvider {
// TODO: read this!
// https://stackoverflow.com/questions/54285472/vscode-how-to-automatically-jump-to-proper-definition
class MarkdownDefinitionProvider implements vscode.DefinitionProvider {
provideDefinition(
public async provideDefinition(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): vscode.ProviderResult<vscode.Definition> {
) {
console.log('provideDefinition');

const p = new vscode.Position(0, 0);

// TODO: paramaterize extensions
const markdownFileRegex = /[\w\.\-\_\/\\]+\.(md|markdown)/i;
const range = document.getWordRangeAtPosition(position, markdownFileRegex);
const selectedWord = document.getText(range);
console.log('selectedWord', selectedWord);
let files: Array<Uri> = [];
// selectedWord might be either:
// a basename for a unique file in the workspace
// or, a relative path to a file
// Since, selectedWord is just a string of text from a document,
// there is no guarantee uniqueFilenamesConfiguration will tell us
// it is not a relative path.
// However, only check for basenames in the entire project if:
if (uniqueFilenamesConfiguration()) {
const filename = selectedWord;
// there should be exactly 1 file with name = selecteWord
files = (await workspace.findFiles('**/*')).filter(f => {
return basename(f.path) == filename;
});
}
// If we did not find any files in the workspace,
// see if a file exists at the relative path:
if (files.length == 0) {
const relativePath = selectedWord;
let fromDir = dirname(document.uri.path.toString());
const absPath = resolve(fromDir, relativePath);
if (existsSync(absPath)) {
const f = Uri.file(absPath);
files.push(f);
}
}

// TODO: find the file named selected-word.md
const workspace = vscode.workspace.getWorkspaceFolder(document.uri);
const root = workspace ? workspace.uri : document.uri;
const f = root.with({
path: join(root.path, 'test.md'),
});
const p = new vscode.Position(0, 0);
const l = new vscode.Location(f, p);
return [l];
return files.map(f => new vscode.Location(f, p));
}
}

Expand Down

0 comments on commit c975a0a

Please sign in to comment.