janja-language-server 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server.d.ts +8 -0
- package/dist/server.js +64 -1
- package/dist/service.d.ts +19 -0
- package/dist/service.js +40 -0
- package/package.json +2 -2
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { type Connection } from 'vscode-languageserver/node';
|
|
2
2
|
export * from './service.js';
|
|
3
|
+
/** The marker file that designates the root of a Janja theme. */
|
|
4
|
+
export declare const ROOT_MARKER = "janja.json";
|
|
5
|
+
/**
|
|
6
|
+
* Walk up from `fileUri` (a `file://` URI) until a directory containing
|
|
7
|
+
* `janja.json` is found. Returns that directory as a URI with a trailing
|
|
8
|
+
* slash, or `null` if none is found before the filesystem root.
|
|
9
|
+
*/
|
|
10
|
+
export declare function findThemeRoot(fileUri: string): Promise<string | null>;
|
|
3
11
|
/**
|
|
4
12
|
* Serve the Language Server Protocol on a connection: diagnostics as
|
|
5
13
|
* documents change, completions, hover and whole-document formatting.
|
package/dist/server.js
CHANGED
|
@@ -1,7 +1,41 @@
|
|
|
1
|
+
import { access } from 'node:fs/promises';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
1
4
|
import { CompletionItemKind, DiagnosticSeverity, InsertTextFormat, MarkupKind, TextDocuments, TextDocumentSyncKind, } from 'vscode-languageserver/node';
|
|
2
5
|
import { TextDocument } from 'vscode-languageserver-textdocument';
|
|
3
|
-
import { formatDocument, getCompletions, getDiagnostics, getHover, } from './service.js';
|
|
6
|
+
import { formatDocument, getCompletions, getDefinition, getDiagnostics, getHover, } from './service.js';
|
|
4
7
|
export * from './service.js';
|
|
8
|
+
/** The marker file that designates the root of a Janja theme. */
|
|
9
|
+
export const ROOT_MARKER = 'janja.json';
|
|
10
|
+
/**
|
|
11
|
+
* Walk up from `fileUri` (a `file://` URI) until a directory containing
|
|
12
|
+
* `janja.json` is found. Returns that directory as a URI with a trailing
|
|
13
|
+
* slash, or `null` if none is found before the filesystem root.
|
|
14
|
+
*/
|
|
15
|
+
export async function findThemeRoot(fileUri) {
|
|
16
|
+
let dir;
|
|
17
|
+
try {
|
|
18
|
+
dir = dirname(fileURLToPath(fileUri));
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
while (true) {
|
|
24
|
+
try {
|
|
25
|
+
await access(`${dir}/${ROOT_MARKER}`);
|
|
26
|
+
return `${pathToFileURL(dir).toString()}/`;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// not here, go up
|
|
30
|
+
}
|
|
31
|
+
const parent = dirname(dir);
|
|
32
|
+
if (parent === dir) {
|
|
33
|
+
// reached the filesystem root without finding the marker
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
dir = parent;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
5
39
|
const completionKinds = {
|
|
6
40
|
directive: CompletionItemKind.Keyword,
|
|
7
41
|
keyword: CompletionItemKind.Operator,
|
|
@@ -19,6 +53,7 @@ export function startServer(connection) {
|
|
|
19
53
|
completionProvider: { triggerCharacters: ['|', ' ', '{'] },
|
|
20
54
|
hoverProvider: true,
|
|
21
55
|
documentFormattingProvider: true,
|
|
56
|
+
definitionProvider: true,
|
|
22
57
|
},
|
|
23
58
|
serverInfo: { name: 'janja-language-server' },
|
|
24
59
|
}));
|
|
@@ -94,6 +129,34 @@ export function startServer(connection) {
|
|
|
94
129
|
},
|
|
95
130
|
];
|
|
96
131
|
});
|
|
132
|
+
connection.onDefinition(async ({ textDocument, position }) => {
|
|
133
|
+
const document = documents.get(textDocument.uri);
|
|
134
|
+
if (!document) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const def = getDefinition(document.getText(), document.offsetAt(position));
|
|
138
|
+
if (!def) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
// Walk up from the document to the nearest janja.json; fall back to the
|
|
142
|
+
// document's own directory when no marker is found.
|
|
143
|
+
const themeRoot = (await findThemeRoot(textDocument.uri)) ??
|
|
144
|
+
new URL('.', textDocument.uri).toString();
|
|
145
|
+
let targetUri;
|
|
146
|
+
try {
|
|
147
|
+
targetUri = new URL(def.path, themeRoot).toString();
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
uri: targetUri,
|
|
154
|
+
range: {
|
|
155
|
+
start: { line: 0, character: 0 },
|
|
156
|
+
end: { line: 0, character: 0 },
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
});
|
|
97
160
|
documents.listen(connection);
|
|
98
161
|
connection.listen();
|
|
99
162
|
}
|
package/dist/service.d.ts
CHANGED
|
@@ -31,6 +31,25 @@ export declare function getCompletions(text: string, offset: number): Completion
|
|
|
31
31
|
* template cannot be formatted (an unclosed tag, typically while typing).
|
|
32
32
|
*/
|
|
33
33
|
export declare function formatDocument(text: string): string | null;
|
|
34
|
+
export interface Definition {
|
|
35
|
+
/**
|
|
36
|
+
* The include path exactly as written in the template (e.g. `./header`).
|
|
37
|
+
* The caller is responsible for resolving it against the document's location.
|
|
38
|
+
*/
|
|
39
|
+
path: string;
|
|
40
|
+
/** Zero-based offset of the first character of the string literal */
|
|
41
|
+
start: number;
|
|
42
|
+
/** Zero-based offset one past the last character of the string literal */
|
|
43
|
+
end: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* When `offset` is inside the path string of an `{{ include "…" }}` or
|
|
47
|
+
* `{{ include '…' }}` directive, return the path and its span so the caller
|
|
48
|
+
* can open the referenced file (cmd+click / Go to Definition).
|
|
49
|
+
*
|
|
50
|
+
* Returns `null` when the cursor is not over an include path.
|
|
51
|
+
*/
|
|
52
|
+
export declare function getDefinition(text: string, offset: number): Definition | null;
|
|
34
53
|
export interface Hover {
|
|
35
54
|
/** Markdown */
|
|
36
55
|
contents: string;
|
package/dist/service.js
CHANGED
|
@@ -105,6 +105,46 @@ export function formatDocument(text) {
|
|
|
105
105
|
return null;
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* When `offset` is inside the path string of an `{{ include "…" }}` or
|
|
110
|
+
* `{{ include '…' }}` directive, return the path and its span so the caller
|
|
111
|
+
* can open the referenced file (cmd+click / Go to Definition).
|
|
112
|
+
*
|
|
113
|
+
* Returns `null` when the cursor is not over an include path.
|
|
114
|
+
*/
|
|
115
|
+
export function getDefinition(text, offset) {
|
|
116
|
+
// Walk backwards to find the nearest opening {{ that is not yet closed
|
|
117
|
+
const open = text.lastIndexOf('{{', offset);
|
|
118
|
+
if (open === -1 || text.lastIndexOf('}}', offset - 1) > open) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
// The text from {{ to the cursor
|
|
122
|
+
const inside = text.slice(open + 2, offset);
|
|
123
|
+
// Must start with an optional strip marker and the word "include"
|
|
124
|
+
if (!/^-?\s*include\s/.test(inside)) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
// Find a string literal that contains the offset.
|
|
128
|
+
// We scan the whole tag so a cursor anywhere on the path string matches.
|
|
129
|
+
const tagEnd = text.indexOf('}}', open);
|
|
130
|
+
const tagText = tagEnd === -1 ? text.slice(open) : text.slice(open, tagEnd + 2);
|
|
131
|
+
// Match either 'path' or "path" in the include directive
|
|
132
|
+
const strRe = /(['"])((?:\\.|[^\\])*?)\1/g;
|
|
133
|
+
let match;
|
|
134
|
+
while ((match = strRe.exec(tagText)) !== null) {
|
|
135
|
+
const litStart = open + match.index;
|
|
136
|
+
const litEnd = litStart + match[0].length;
|
|
137
|
+
// The cursor must land somewhere inside (or on the edges of) the literal
|
|
138
|
+
if (offset >= litStart && offset <= litEnd) {
|
|
139
|
+
return {
|
|
140
|
+
path: match[2],
|
|
141
|
+
start: litStart,
|
|
142
|
+
end: litEnd,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
108
148
|
const directiveDocs = {
|
|
109
149
|
if: '`{{ if test }}...{{ elif test }}...{{ else }}...{{ endif }}`\n\nRenders the first branch whose test is truthy.',
|
|
110
150
|
for: '`{{ for item of items }}...{{ else }}...{{ endfor }}`\n\nIterates arrays, strings and other iterables. `loop.index`, `loop.first` and `loop.last` are available in the body; `else` renders when there is nothing to iterate.',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "janja-language-server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Language server for Janja templates: diagnostics, completions and formatting",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"README.md"
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"janja": "^4.
|
|
43
|
+
"janja": "^4.5.0",
|
|
44
44
|
"vscode-languageserver": "^10.1.2",
|
|
45
45
|
"vscode-languageserver-textdocument": "^1.0.15"
|
|
46
46
|
},
|