├── .gitignore ├── .assets └── vid1.mov ├── server ├── package.json ├── src │ └── server.ts ├── tsconfig.json └── package-lock.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | server/out/ 3 | -------------------------------------------------------------------------------- /.assets/vid1.mov: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danymat/lsp-zettelkasten/HEAD/.assets/vid1.mov -------------------------------------------------------------------------------- /server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "@types/node": "^15.0.2", 4 | "tree-sitter-cli": "^0.19.5", 5 | "typescript": "^4.2.4" 6 | }, 7 | "dependencies": { 8 | "nan": "^2.14.2", 9 | "vscode-languageserver": "^7.0.0", 10 | "vscode-languageserver-textdocument": "^1.0.1" 11 | }, 12 | "scripts": { 13 | "compile": "tsc -b ./tsconfig.json", 14 | "watch": "tsc -b --watch ./tsconfig.json" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lsp-zettelkasten 2 | 3 | This repo is a basic implementation of a lsp for zettelkasten markdown files 4 | 5 | ![movie-1](https://github.com/lsp-zettelkasten/lsp-zettelkasten/blob/main/.assets/vid1.mov) 6 | 7 | 8 | ## Features 9 | 10 | - ✔️ Support for link completion for type `[[link here]]`. 11 | - 🚧 Clickable links inside `[[..]]`. 12 | - ✔️ Support for file content preview in completion (with onCompletionResolve). 13 | - 🚧 Hover option on clickable links, in order to preview the file. 14 | - 🚧 Support tag completions with `#` character (needs a grammar). 15 | 16 | ## Installation 17 | 18 | ```bash 19 | git clone https://github.com/lsp-zettelkasten/lsp-zettelkasten.git 20 | cd lsp-zettelkasten/server 21 | npm install 22 | npm run compile 23 | ``` 24 | 25 | After compiling, the compiled javascript will be in `out` directory. 26 | 27 | ## Usage 28 | 29 | The command to start the lsp is `node out/server.js --stdio`. 30 | 31 | However, you'll need a working client for your text editor. 32 | 33 | - Neovim (using [lspconfig](https://github.com/lsp-zettelkasten/lsp-zettelkasten.git)) 34 | 35 |
~/.config/nvim/init.lua 36 | 37 | ```lua 38 | local lspconfig = require'lspconfig' 39 | local configs = require'lspconfig/configs' 40 | 41 | if not lspconfig.zettelkastenlsp then 42 | configs.zettelkastenlsp = { 43 | default_config = { 44 | cmd = {'node', 'path/to/lsp-zettelkasten/out/server.js', '--stdio'}; 45 | filetypes = {'markdown'}; 46 | root_dir = function(fname) 47 | return lspconfig.util.find_git_ancestor(fname) or vim.loop.os_homedir() 48 | end; 49 | settings = {}; 50 | }; 51 | } 52 | end 53 | lspconfig.zettelkastenlsp.setup{} 54 | ``` 55 | 56 |
57 | -------------------------------------------------------------------------------- /server/src/server.ts: -------------------------------------------------------------------------------- 1 | import { TextDocuments, createConnection, InitializeResult, ProposedFeatures, 2 | TextDocumentSyncKind, Hover, CompletionList, CompletionParams, CompletionItem, 3 | MarkupKind, 4 | WorkspaceFolder} from "vscode-languageserver/node"; 5 | 6 | import { TextDocument } from "vscode-languageserver-textdocument"; 7 | import { readdirSync, readFileSync } from "fs" 8 | 9 | const documents = new TextDocuments(TextDocument); 10 | let connection = createConnection(ProposedFeatures.all); 11 | let workspaceFolders: WorkspaceFolder[] = [] 12 | 13 | connection.onInitialize((params) => { 14 | params.workspaceFolders 15 | workspaceFolders = params.workspaceFolders! 16 | const result: InitializeResult = { 17 | capabilities: { 18 | textDocumentSync : TextDocumentSyncKind.Incremental, 19 | completionProvider: { 20 | resolveProvider: true, 21 | triggerCharacters: ["#", "["] 22 | } 23 | //hoverProvider: true 24 | } 25 | } 26 | return result; 27 | }) 28 | 29 | connection.onInitialized(() => { 30 | connection.console.log("Initialized"); 31 | }) 32 | 33 | connection.onCompletion((params: CompletionParams) => { 34 | let line = params.position.line 35 | let char = params.position.character 36 | let messages: CompletionList = { isIncomplete: false, items : [] } 37 | 38 | let document = documents.get(params.textDocument.uri) 39 | if (params.context?.triggerCharacter == "#") { 40 | 41 | // Creating messages 42 | // // TODO 43 | let item: CompletionItem = { label: "#foo" } 44 | messages.items.push(item) 45 | 46 | // Resets if the user press space button 47 | if ( document ) { 48 | let text = document.getText({ 49 | start: {line: line , character: char - 1}, 50 | end: {line: line, character: char}, 51 | }) 52 | if (text == " ") { return null } 53 | } 54 | } 55 | else { 56 | if (document) { 57 | let text = document.getText({ 58 | start: {line: line, character: char - 2 }, 59 | end: {line: line, character: char} 60 | }) 61 | let beforeText = document.getText({ 62 | start: { line: line, character: char - 3 }, 63 | end: { line: line, character: char - 2 } 64 | }) 65 | if ( beforeText == '[' ) { return null } 66 | else if ( text == '[[' ) { 67 | let item: CompletionItem = { label: "" } 68 | getFileNames().forEach((file) => { 69 | item = {label: file } 70 | messages.items.push(item) 71 | }) 72 | } 73 | else return null 74 | } 75 | } 76 | return messages 77 | }) 78 | 79 | function getFileNames() { 80 | let uri = workspaceFolders.map((ws) => ws.name) 81 | let files: string[] = [] 82 | uri.forEach(u => { 83 | readdirSync(u).forEach(file => { 84 | if ( !file.startsWith('.') ) 85 | files.push(file.slice(0, -3)) 86 | }) 87 | }) 88 | return files 89 | } 90 | 91 | connection.onCompletionResolve((item): CompletionItem => { 92 | let file = readFileSync(`${item.label}.md`) 93 | item.detail = item.label 94 | item.documentation = { 95 | kind: MarkupKind.Markdown, 96 | value: file.toString() 97 | } 98 | return item; 99 | }); 100 | 101 | documents.onWillSave((event) => { 102 | connection.console.log('On Will save received'); 103 | }); 104 | 105 | documents.listen(connection); 106 | connection.listen(); 107 | -------------------------------------------------------------------------------- /server/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig.json to read more about this file */ 4 | 5 | /* Basic Options */ 6 | // "incremental": true, /* Enable incremental compilation */ 7 | "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ 8 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ 9 | // "lib": [], /* Specify library files to be included in the compilation. */ 10 | // "allowJs": true, /* Allow javascript files to be compiled. */ 11 | // "checkJs": true, /* Report errors in .js files. */ 12 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */ 13 | // "declaration": true, /* Generates corresponding '.d.ts' file. */ 14 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 15 | // "sourceMap": true, /* Generates corresponding '.map' file. */ 16 | // "outFile": "./", /* Concatenate and emit output to single file. */ 17 | "outDir": "./out/", /* Redirect output structure to the directory. */ 18 | "rootDir": "./src/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 19 | // "composite": true, /* Enable project compilation */ 20 | // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ 21 | // "removeComments": true, /* Do not emit comments to output. */ 22 | // "noEmit": true, /* Do not emit outputs. */ 23 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 24 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 25 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 26 | 27 | /* Strict Type-Checking Options */ 28 | "strict": true, /* Enable all strict type-checking options. */ 29 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 30 | // "strictNullChecks": true, /* Enable strict null checks. */ 31 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 32 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ 33 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 34 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 35 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 36 | 37 | /* Additional Checks */ 38 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 39 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 40 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 41 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 42 | // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ 43 | // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */ 44 | 45 | /* Module Resolution Options */ 46 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 47 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 48 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 49 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 50 | // "typeRoots": [], /* List of folders to include type definitions from. */ 51 | // "types": [], /* Type declaration files to be included in compilation. */ 52 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 53 | "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 54 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 55 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 56 | 57 | /* Source Map Options */ 58 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 59 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 60 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 61 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 62 | 63 | /* Experimental Options */ 64 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 65 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 66 | 67 | /* Advanced Options */ 68 | "skipLibCheck": true, /* Skip type checking of declaration files. */ 69 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /server/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server", 3 | "lockfileVersion": 2, 4 | "requires": true, 5 | "packages": { 6 | "": { 7 | "dependencies": { 8 | "nan": "^2.14.2", 9 | "vscode-languageserver": "^7.0.0", 10 | "vscode-languageserver-textdocument": "^1.0.1" 11 | }, 12 | "devDependencies": { 13 | "@types/node": "^15.0.2", 14 | "tree-sitter-cli": "^0.19.5", 15 | "typescript": "^4.2.4" 16 | } 17 | }, 18 | "node_modules/@types/node": { 19 | "version": "15.0.2", 20 | "resolved": "https://registry.npmjs.org/@types/node/-/node-15.0.2.tgz", 21 | "integrity": "sha512-p68+a+KoxpoB47015IeYZYRrdqMUcpbK8re/zpFB8Ld46LHC1lPEbp3EXgkEhAYEcPvjJF6ZO+869SQ0aH1dcA==", 22 | "dev": true 23 | }, 24 | "node_modules/nan": { 25 | "version": "2.14.2", 26 | "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz", 27 | "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==" 28 | }, 29 | "node_modules/tree-sitter-cli": { 30 | "version": "0.19.5", 31 | "resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.19.5.tgz", 32 | "integrity": "sha512-kRzKrUAwpDN9AjA3b0tPBwT1hd8N2oQvvvHup2OEsX6mdsSMLmAvR+NSqK9fe05JrRbVvG8mbteNUQsxlMQohQ==", 33 | "dev": true, 34 | "hasInstallScript": true, 35 | "bin": { 36 | "tree-sitter": "cli.js" 37 | } 38 | }, 39 | "node_modules/typescript": { 40 | "version": "4.2.4", 41 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.4.tgz", 42 | "integrity": "sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg==", 43 | "dev": true, 44 | "bin": { 45 | "tsc": "bin/tsc", 46 | "tsserver": "bin/tsserver" 47 | }, 48 | "engines": { 49 | "node": ">=4.2.0" 50 | } 51 | }, 52 | "node_modules/vscode-jsonrpc": { 53 | "version": "6.0.0", 54 | "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-6.0.0.tgz", 55 | "integrity": "sha512-wnJA4BnEjOSyFMvjZdpiOwhSq9uDoK8e/kpRJDTaMYzwlkrhG1fwDIZI94CLsLzlCK5cIbMMtFlJlfR57Lavmg==", 56 | "engines": { 57 | "node": ">=8.0.0 || >=10.0.0" 58 | } 59 | }, 60 | "node_modules/vscode-languageserver": { 61 | "version": "7.0.0", 62 | "resolved": "https://registry.npmjs.org/vscode-languageserver/-/vscode-languageserver-7.0.0.tgz", 63 | "integrity": "sha512-60HTx5ID+fLRcgdHfmz0LDZAXYEV68fzwG0JWwEPBode9NuMYTIxuYXPg4ngO8i8+Ou0lM7y6GzaYWbiDL0drw==", 64 | "dependencies": { 65 | "vscode-languageserver-protocol": "3.16.0" 66 | }, 67 | "bin": { 68 | "installServerIntoExtension": "bin/installServerIntoExtension" 69 | } 70 | }, 71 | "node_modules/vscode-languageserver-protocol": { 72 | "version": "3.16.0", 73 | "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.16.0.tgz", 74 | "integrity": "sha512-sdeUoAawceQdgIfTI+sdcwkiK2KU+2cbEYA0agzM2uqaUy2UpnnGHtWTHVEtS0ES4zHU0eMFRGN+oQgDxlD66A==", 75 | "dependencies": { 76 | "vscode-jsonrpc": "6.0.0", 77 | "vscode-languageserver-types": "3.16.0" 78 | } 79 | }, 80 | "node_modules/vscode-languageserver-textdocument": { 81 | "version": "1.0.1", 82 | "resolved": "https://registry.npmjs.org/vscode-languageserver-textdocument/-/vscode-languageserver-textdocument-1.0.1.tgz", 83 | "integrity": "sha512-UIcJDjX7IFkck7cSkNNyzIz5FyvpQfY7sdzVy+wkKN/BLaD4DQ0ppXQrKePomCxTS7RrolK1I0pey0bG9eh8dA==" 84 | }, 85 | "node_modules/vscode-languageserver-types": { 86 | "version": "3.16.0", 87 | "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0.tgz", 88 | "integrity": "sha512-k8luDIWJWyenLc5ToFQQMaSrqCHiLwyKPHKPQZ5zz21vM+vIVUSvsRpcbiECH4WR88K2XZqc4ScRcZ7nk/jbeA==" 89 | } 90 | }, 91 | "dependencies": { 92 | "@types/node": { 93 | "version": "15.0.2", 94 | "resolved": "https://registry.npmjs.org/@types/node/-/node-15.0.2.tgz", 95 | "integrity": "sha512-p68+a+KoxpoB47015IeYZYRrdqMUcpbK8re/zpFB8Ld46LHC1lPEbp3EXgkEhAYEcPvjJF6ZO+869SQ0aH1dcA==", 96 | "dev": true 97 | }, 98 | "nan": { 99 | "version": "2.14.2", 100 | "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz", 101 | "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==" 102 | }, 103 | "tree-sitter-cli": { 104 | "version": "0.19.5", 105 | "resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.19.5.tgz", 106 | "integrity": "sha512-kRzKrUAwpDN9AjA3b0tPBwT1hd8N2oQvvvHup2OEsX6mdsSMLmAvR+NSqK9fe05JrRbVvG8mbteNUQsxlMQohQ==", 107 | "dev": true 108 | }, 109 | "typescript": { 110 | "version": "4.2.4", 111 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.4.tgz", 112 | "integrity": "sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg==", 113 | "dev": true 114 | }, 115 | "vscode-jsonrpc": { 116 | "version": "6.0.0", 117 | "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-6.0.0.tgz", 118 | "integrity": "sha512-wnJA4BnEjOSyFMvjZdpiOwhSq9uDoK8e/kpRJDTaMYzwlkrhG1fwDIZI94CLsLzlCK5cIbMMtFlJlfR57Lavmg==" 119 | }, 120 | "vscode-languageserver": { 121 | "version": "7.0.0", 122 | "resolved": "https://registry.npmjs.org/vscode-languageserver/-/vscode-languageserver-7.0.0.tgz", 123 | "integrity": "sha512-60HTx5ID+fLRcgdHfmz0LDZAXYEV68fzwG0JWwEPBode9NuMYTIxuYXPg4ngO8i8+Ou0lM7y6GzaYWbiDL0drw==", 124 | "requires": { 125 | "vscode-languageserver-protocol": "3.16.0" 126 | } 127 | }, 128 | "vscode-languageserver-protocol": { 129 | "version": "3.16.0", 130 | "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.16.0.tgz", 131 | "integrity": "sha512-sdeUoAawceQdgIfTI+sdcwkiK2KU+2cbEYA0agzM2uqaUy2UpnnGHtWTHVEtS0ES4zHU0eMFRGN+oQgDxlD66A==", 132 | "requires": { 133 | "vscode-jsonrpc": "6.0.0", 134 | "vscode-languageserver-types": "3.16.0" 135 | } 136 | }, 137 | "vscode-languageserver-textdocument": { 138 | "version": "1.0.1", 139 | "resolved": "https://registry.npmjs.org/vscode-languageserver-textdocument/-/vscode-languageserver-textdocument-1.0.1.tgz", 140 | "integrity": "sha512-UIcJDjX7IFkck7cSkNNyzIz5FyvpQfY7sdzVy+wkKN/BLaD4DQ0ppXQrKePomCxTS7RrolK1I0pey0bG9eh8dA==" 141 | }, 142 | "vscode-languageserver-types": { 143 | "version": "3.16.0", 144 | "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0.tgz", 145 | "integrity": "sha512-k8luDIWJWyenLc5ToFQQMaSrqCHiLwyKPHKPQZ5zz21vM+vIVUSvsRpcbiECH4WR88K2XZqc4ScRcZ7nk/jbeA==" 146 | } 147 | } 148 | } 149 | --------------------------------------------------------------------------------