janja-language-server 0.1.11 → 0.1.13

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.js CHANGED
@@ -1,12 +1,13 @@
1
- import { access } from 'node:fs/promises';
1
+ import { access, readFile } from 'node:fs/promises';
2
2
  import { dirname, extname } from 'node:path';
3
3
  import { fileURLToPath, pathToFileURL } from 'node:url';
4
+ import { parseProjectFilters, PROJECT_CONFIG } from 'janja';
4
5
  import { CompletionItemKind, DiagnosticSeverity, InsertTextFormat, MarkupKind, TextDocuments, TextDocumentSyncKind, } from 'vscode-languageserver/node';
5
6
  import { TextDocument } from 'vscode-languageserver-textdocument';
6
7
  import { formatDocument, getCompletions, getDefinition, getDiagnostics, getHover, } from './service.js';
7
8
  export * from './service.js';
8
9
  /** The marker file that designates the root of a Janja theme. */
9
- export const ROOT_MARKER = 'janja.json';
10
+ export const ROOT_MARKER = PROJECT_CONFIG;
10
11
  /**
11
12
  * Walk up from `fileUri` (a `file://` URI) until a directory containing
12
13
  * `janja.json` is found. Returns that directory as a URI with a trailing
@@ -36,6 +37,18 @@ export async function findThemeRoot(fileUri) {
36
37
  dir = parent;
37
38
  }
38
39
  }
40
+ async function getProjectFilters(uri) {
41
+ const root = await findThemeRoot(uri);
42
+ if (!root) {
43
+ return [];
44
+ }
45
+ try {
46
+ return parseProjectFilters(await readFile(new URL(ROOT_MARKER, root), 'utf8'));
47
+ }
48
+ catch {
49
+ return [];
50
+ }
51
+ }
39
52
  const completionKinds = {
40
53
  directive: CompletionItemKind.Keyword,
41
54
  keyword: CompletionItemKind.Operator,
@@ -59,7 +72,7 @@ export function startServer(connection) {
59
72
  }));
60
73
  const validate = async (document) => {
61
74
  const { version } = document;
62
- const found = await getDiagnostics(document.getText());
75
+ const found = await getDiagnostics(document.getText(), await getProjectFilters(document.uri));
63
76
  // The document may have changed, or been closed, while it was checked
64
77
  if (documents.get(document.uri)?.version !== version) {
65
78
  return;
@@ -85,12 +98,12 @@ export function startServer(connection) {
85
98
  documents.onDidClose(({ document }) => {
86
99
  void connection.sendDiagnostics({ uri: document.uri, diagnostics: [] });
87
100
  });
88
- connection.onCompletion(({ textDocument, position }) => {
101
+ connection.onCompletion(async ({ textDocument, position }) => {
89
102
  const document = documents.get(textDocument.uri);
90
103
  if (!document) {
91
104
  return [];
92
105
  }
93
- return getCompletions(document.getText(), document.offsetAt(position)).map(({ label, kind, detail, snippet }) => ({
106
+ return getCompletions(document.getText(), document.offsetAt(position), await getProjectFilters(document.uri)).map(({ label, kind, detail, snippet }) => ({
94
107
  label,
95
108
  kind: completionKinds[kind],
96
109
  detail,
package/dist/service.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { parseProjectFilters, PROJECT_CONFIG } from 'janja';
1
2
  /**
2
3
  * Editor features as plain functions over text, independent of any editor
3
4
  * or protocol.
@@ -19,13 +20,13 @@ export interface Completion {
19
20
  /** Snippet inserted instead of the label, with `$1`-style placeholders */
20
21
  snippet?: string;
21
22
  }
22
- export declare function getDiagnostics(text: string): Promise<Diagnostic[]>;
23
+ export declare function getDiagnostics(text: string, projectFilters?: readonly string[]): Promise<Diagnostic[]>;
23
24
  /**
24
25
  * What can be typed at `offset`: filter names after a pipe, directive names
25
26
  * at the start of a `{{ }}` tag, expression keywords elsewhere inside a tag,
26
27
  * and nothing in plain text or comments.
27
28
  */
28
- export declare function getCompletions(text: string, offset: number): Completion[];
29
+ export declare function getCompletions(text: string, offset: number, projectFilters?: readonly string[]): Completion[];
29
30
  /**
30
31
  * The formatted text, or `null` when there is nothing to change or the
31
32
  * template cannot be formatted (an unclosed tag, typically while typing).
package/dist/service.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // Snippets use VS Code's `${1:placeholder}` syntax in ordinary strings
2
2
  /* eslint-disable no-template-curly-in-string */
3
- import { filters as builtinFilters, formatTemplate, validateTemplate, } from 'janja';
3
+ import { filters as builtinFilters, formatTemplate, validateTemplate, validationFilters, } from 'janja';
4
+ export { parseProjectFilters, PROJECT_CONFIG } from 'janja';
4
5
  const directives = [
5
6
  ['if', 'if ${1:condition} }}$0{{ endif'],
6
7
  ['elif', 'elif ${1:condition}'],
@@ -51,8 +52,10 @@ const filters = Object.keys(builtinFilters)
51
52
  .filter((name) => name !== 'escape')
52
53
  .sort()
53
54
  .map((label) => ({ label, kind: 'filter', detail: 'built-in filter' }));
54
- export async function getDiagnostics(text) {
55
- const { errors, warnings } = await validateTemplate(text);
55
+ export async function getDiagnostics(text, projectFilters = []) {
56
+ const { errors, warnings } = await validateTemplate(text, {
57
+ filters: validationFilters(projectFilters),
58
+ });
56
59
  const lines = text.split('\n');
57
60
  return [...errors, ...warnings].map(({ type, message, line, column }) => {
58
61
  const zeroLine = Math.max((line ?? 1) - 1, 0);
@@ -74,7 +77,7 @@ export async function getDiagnostics(text) {
74
77
  * at the start of a `{{ }}` tag, expression keywords elsewhere inside a tag,
75
78
  * and nothing in plain text or comments.
76
79
  */
77
- export function getCompletions(text, offset) {
80
+ export function getCompletions(text, offset, projectFilters = []) {
78
81
  const before = text.slice(0, offset);
79
82
  const open = before.lastIndexOf('{{');
80
83
  if (open === -1 || before.lastIndexOf('}}') > open) {
@@ -85,7 +88,16 @@ export function getCompletions(text, offset) {
85
88
  return [];
86
89
  }
87
90
  if (/\|\s*[\w$]*$/.test(inside)) {
88
- return filters;
91
+ return [
92
+ ...filters,
93
+ ...projectFilters
94
+ .filter((name) => !Object.hasOwn(builtinFilters, name))
95
+ .map((label) => ({
96
+ label,
97
+ kind: 'filter',
98
+ detail: 'project filter',
99
+ })),
100
+ ];
89
101
  }
90
102
  if (!inside.startsWith('=') && /^-?\s*[A-Za-z_]*$/.test(inside)) {
91
103
  return directives;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "janja-language-server",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
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.9.0",
43
+ "janja": "^4.11.0",
44
44
  "vscode-languageserver": "^10.1.2",
45
45
  "vscode-languageserver-textdocument": "^1.0.15"
46
46
  },