janja-language-server 0.1.5 → 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 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
@@ -11,7 +11,7 @@ const directives = [
11
11
  ['break', 'break'],
12
12
  ['continue', 'continue'],
13
13
  ['set', 'set ${1:name} = ${2:value}'],
14
- ['include', 'include "${1:path}"'],
14
+ ['include', 'include "${1:path}"$0'],
15
15
  ['block', 'block ${1:name} }}$0{{ endblock'],
16
16
  ['endblock', 'endblock'],
17
17
  ['super', 'super'],
@@ -24,7 +24,7 @@ const directives = [
24
24
  ['endcapture', 'endcapture'],
25
25
  ['with', 'with ${1:value} }}$0{{ endwith'],
26
26
  ['endwith', 'endwith'],
27
- ['stylesheet', 'stylesheet }}$0{{ endstylesheet'],
27
+ ['stylesheet', 'stylesheet }}\n<style>\n$0\n</style>\n{{ endstylesheet'],
28
28
  ['endstylesheet', 'endstylesheet'],
29
29
  ['stylesheets', 'stylesheets'],
30
30
  ].map(([label, snippet]) => ({ label, kind: 'directive', snippet }));
@@ -105,19 +105,59 @@ 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.',
111
151
  set: '`{{ set name = expression }}`\n\nAssigns in the current scope. `{{ set (a, b) = value }}` reads properties.',
112
- include: '`{{ include "path" }}`\n\nRenders another template in place, loaded through the `loader` option.',
152
+ include: '`{{ include path }}` / `{{ include path with expr }}`\n\nRenders another template in place. `path` may be a string literal or any expression; dynamic paths need `preloadIncludes`.',
113
153
  block: '`{{ block name }}...{{ endblock }}`\n\nA replaceable region. The last definition of a name is rendered where the block first appears; `{{ super }}` renders the definition it replaced.',
114
154
  macro: '`{{ macro name(param, other = default) }}...{{ endmacro }}`\n\nA reusable piece of template, invoked with `{{ call name(args) }}...{{ endcall }}`. `{{ caller }}` renders the body of the call.',
115
155
  call: '`{{ call name(args) }}...{{ endcall }}`\n\nInvokes a macro; the body is rendered where the macro has `{{ caller }}`.',
116
156
  capture: '`{{ capture name }}...{{ endcapture }}`\n\nRenders the body into a variable instead of the output.',
117
157
  with: '`{{ with value }}...{{ endwith }}`\n\nRenders the body in a scope made of the properties of `value` only.',
118
- stylesheet: '`{{ stylesheet }}...{{ endstylesheet }}`\n\nCo-locates CSS with a section or snippet (Shopify `{% stylesheet %}`). Collected at compile time; writes nothing where it appears.',
158
+ stylesheet: '`{{ stylesheet }}...{{ endstylesheet }}`\n\nCo-locates styles with a section or snippet (Shopify `{% stylesheet %}`). Put a `<style>` in the body for native IDE CSS highlighting. Collected at compile time; writes nothing where it appears.',
119
159
  endstylesheet: '`{{ endstylesheet }}`\n\nCloses a `stylesheet` block.',
120
- stylesheets: '`{{ stylesheets }}`\n\nEmits a `<style>` with every collected stylesheet body. Place in the layout `<head>`; only the first occurrence runs.',
160
+ stylesheets: '`{{ stylesheets }}`\n\nConcatenates every collected stylesheet body as-is (no extra `<style>` wrap). Place in the layout `<head>`; only the first occurrence runs.',
121
161
  break: '`{{ break }}`\n\nEnds the innermost loop.',
122
162
  continue: '`{{ continue }}`\n\nSkips to the next iteration of the innermost loop.',
123
163
  super: '`{{ super }}`\n\nInside a block, renders the definition this block replaced.',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "janja-language-server",
3
- "version": "0.1.5",
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.3.0",
43
+ "janja": "^4.5.0",
44
44
  "vscode-languageserver": "^10.1.2",
45
45
  "vscode-languageserver-textdocument": "^1.0.15"
46
46
  },