@getpaseo/highlight 0.2.0-beta.1 → 0.2.0-beta.3

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.
@@ -1,48 +1,6 @@
1
- import { highlightTree, tagHighlighter, tags } from "@lezer/highlight";
1
+ import { highlightTree } from "@lezer/highlight";
2
2
  import { getParserForFile } from "./parsers.js";
3
- const highlighter = tagHighlighter([
4
- { tag: tags.keyword, class: "keyword" },
5
- { tag: tags.controlKeyword, class: "keyword" },
6
- { tag: tags.operatorKeyword, class: "keyword" },
7
- { tag: tags.definitionKeyword, class: "keyword" },
8
- { tag: tags.moduleKeyword, class: "keyword" },
9
- { tag: tags.comment, class: "comment" },
10
- { tag: tags.lineComment, class: "comment" },
11
- { tag: tags.blockComment, class: "comment" },
12
- { tag: tags.docComment, class: "comment" },
13
- { tag: tags.string, class: "string" },
14
- { tag: tags.special(tags.string), class: "string" },
15
- { tag: tags.number, class: "number" },
16
- { tag: tags.integer, class: "number" },
17
- { tag: tags.float, class: "number" },
18
- { tag: tags.bool, class: "literal" },
19
- { tag: tags.null, class: "literal" },
20
- { tag: tags.function(tags.variableName), class: "function" },
21
- { tag: tags.function(tags.propertyName), class: "function" },
22
- { tag: tags.definition(tags.variableName), class: "definition" },
23
- { tag: tags.definition(tags.propertyName), class: "definition" },
24
- { tag: tags.definition(tags.function(tags.variableName)), class: "definition" },
25
- { tag: tags.className, class: "class" },
26
- { tag: tags.definition(tags.className), class: "class" },
27
- { tag: tags.typeName, class: "type" },
28
- { tag: tags.tagName, class: "tag" },
29
- { tag: tags.attributeName, class: "attribute" },
30
- { tag: tags.attributeValue, class: "string" },
31
- { tag: tags.propertyName, class: "property" },
32
- { tag: tags.variableName, class: "variable" },
33
- { tag: tags.local(tags.variableName), class: "variable" },
34
- { tag: tags.special(tags.variableName), class: "variable" },
35
- { tag: tags.operator, class: "operator" },
36
- { tag: tags.punctuation, class: "punctuation" },
37
- { tag: tags.bracket, class: "punctuation" },
38
- { tag: tags.separator, class: "punctuation" },
39
- { tag: tags.regexp, class: "regexp" },
40
- { tag: tags.escape, class: "escape" },
41
- { tag: tags.meta, class: "meta" },
42
- { tag: tags.heading, class: "heading" },
43
- { tag: tags.link, class: "link" },
44
- { tag: tags.url, class: "link" },
45
- ]);
3
+ import { staticSyntaxHighlighter } from "./syntax-roles.js";
46
4
  export function highlightCode(code, filename) {
47
5
  const parser = getParserForFile(filename);
48
6
  if (!parser) {
@@ -56,7 +14,7 @@ export function highlightCode(code, filename) {
56
14
  }
57
15
  // Build a map of character positions to styles
58
16
  const styleMap = Array.from({ length: code.length }, () => null);
59
- highlightTree(tree, highlighter, (from, to, classes) => {
17
+ highlightTree(tree, staticSyntaxHighlighter, (from, to, classes) => {
60
18
  for (let i = from; i < to && i < styleMap.length; i++) {
61
19
  styleMap[i] = classes;
62
20
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export type { HighlightStyle, HighlightToken } from "./types.js";
2
- export { getParserForFile, isLanguageSupported, getSupportedExtensions } from "./parsers.js";
2
+ export { getLanguageForFile, getParserForFile, isLanguageSupported, getSupportedExtensions, } from "./parsers.js";
3
+ export { createCodeMirrorHighlightStyle } from "./syntax-roles.js";
3
4
  export { highlightCode, highlightLine } from "./highlighter.js";
4
5
  export { darkHighlightColors, lightHighlightColors } from "./colors.js";
5
6
  export type { SyntaxThemeId, SyntaxThemeOption, SyntaxColors } from "./themes.js";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- export { getParserForFile, isLanguageSupported, getSupportedExtensions } from "./parsers.js";
1
+ export { getLanguageForFile, getParserForFile, isLanguageSupported, getSupportedExtensions, } from "./parsers.js";
2
+ export { createCodeMirrorHighlightStyle } from "./syntax-roles.js";
2
3
  export { highlightCode, highlightLine } from "./highlighter.js";
3
4
  export { darkHighlightColors, lightHighlightColors } from "./colors.js";
4
5
  export { SYNTAX_THEME_IDS, SYNTAX_THEME_OPTIONS, isSyntaxThemeId, resolveSyntaxColors, } from "./themes.js";
package/dist/parsers.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import { Language } from "@codemirror/language";
1
2
  import type { Parser } from "@lezer/common";
3
+ export declare function getLanguageForFile(filename: string): Language | null;
2
4
  export declare function getParserForFile(filename: string): Parser | null;
3
5
  export declare function isLanguageSupported(filename: string): boolean;
4
6
  export declare function getSupportedExtensions(): string[];
package/dist/parsers.js CHANGED
@@ -1,4 +1,4 @@
1
- import { StreamLanguage } from "@codemirror/language";
1
+ import { defineLanguageFacet, Language, StreamLanguage } from "@codemirror/language";
2
2
  import { dart } from "@codemirror/legacy-modes/mode/clike";
3
3
  import { swift } from "@codemirror/legacy-modes/mode/swift";
4
4
  import { parser as jsParser } from "@lezer/javascript";
@@ -16,70 +16,76 @@ import { parser as xmlParser } from "@lezer/xml";
16
16
  import { parser as yamlParser } from "@lezer/yaml";
17
17
  import { csharpLanguage } from "@replit/codemirror-lang-csharp";
18
18
  import { parser as elixirParser } from "lezer-elixir";
19
- const parsersByExtension = {
19
+ function language(parser) {
20
+ return new Language(defineLanguageFacet(), parser);
21
+ }
22
+ const languagesByExtension = {
20
23
  // JavaScript/TypeScript
21
- js: jsParser,
22
- jsx: jsParser.configure({ dialect: "jsx" }),
23
- ts: jsParser.configure({ dialect: "ts" }),
24
- tsx: jsParser.configure({ dialect: "ts jsx" }),
25
- mjs: jsParser,
26
- cjs: jsParser,
24
+ js: language(jsParser),
25
+ jsx: language(jsParser.configure({ dialect: "jsx" })),
26
+ ts: language(jsParser.configure({ dialect: "ts" })),
27
+ tsx: language(jsParser.configure({ dialect: "ts jsx" })),
28
+ mjs: language(jsParser),
29
+ cjs: language(jsParser),
27
30
  // C / C++ / Objective-C
28
- c: cppParser,
29
- h: cppParser,
30
- cc: cppParser,
31
- cpp: cppParser,
32
- cxx: cppParser,
33
- hpp: cppParser,
34
- hxx: cppParser,
35
- m: cppParser,
36
- mm: cppParser,
31
+ c: language(cppParser),
32
+ h: language(cppParser),
33
+ cc: language(cppParser),
34
+ cpp: language(cppParser),
35
+ cxx: language(cppParser),
36
+ hpp: language(cppParser),
37
+ hxx: language(cppParser),
38
+ m: language(cppParser),
39
+ mm: language(cppParser),
37
40
  // JSON
38
- json: jsonParser,
41
+ json: language(jsonParser),
39
42
  // CSS
40
- css: cssParser,
41
- scss: cssParser,
43
+ css: language(cssParser),
44
+ scss: language(cssParser),
42
45
  // HTML
43
- html: htmlParser,
44
- htm: htmlParser,
46
+ html: language(htmlParser),
47
+ htm: language(htmlParser),
45
48
  // XML
46
- xml: xmlParser,
49
+ xml: language(xmlParser),
47
50
  // Java
48
- java: javaParser,
51
+ java: language(javaParser),
49
52
  // Python
50
- py: pythonParser,
53
+ py: language(pythonParser),
51
54
  // Go
52
- go: goParser,
55
+ go: language(goParser),
53
56
  // PHP
54
- php: phpParser,
57
+ php: language(phpParser),
55
58
  // YAML
56
- yaml: yamlParser,
57
- yml: yamlParser,
59
+ yaml: language(yamlParser),
60
+ yml: language(yamlParser),
58
61
  // Rust
59
- rs: rustParser,
62
+ rs: language(rustParser),
60
63
  // Swift
61
- swift: StreamLanguage.define(swift).parser,
64
+ swift: StreamLanguage.define(swift),
62
65
  // Dart
63
- dart: StreamLanguage.define(dart).parser,
66
+ dart: StreamLanguage.define(dart),
64
67
  // C#
65
- cs: csharpLanguage.parser,
68
+ cs: csharpLanguage,
66
69
  // Elixir
67
- ex: elixirParser,
68
- exs: elixirParser,
70
+ ex: language(elixirParser),
71
+ exs: language(elixirParser),
69
72
  // Markdown
70
- md: markdownParser,
71
- mdx: markdownParser,
73
+ md: language(markdownParser),
74
+ mdx: language(markdownParser),
72
75
  };
73
- export function getParserForFile(filename) {
76
+ export function getLanguageForFile(filename) {
74
77
  const ext = filename.split(".").pop()?.toLowerCase();
75
78
  if (!ext)
76
79
  return null;
77
- return parsersByExtension[ext] ?? null;
80
+ return languagesByExtension[ext] ?? null;
81
+ }
82
+ export function getParserForFile(filename) {
83
+ return getLanguageForFile(filename)?.parser ?? null;
78
84
  }
79
85
  export function isLanguageSupported(filename) {
80
86
  return getParserForFile(filename) !== null;
81
87
  }
82
88
  export function getSupportedExtensions() {
83
- return Object.keys(parsersByExtension);
89
+ return Object.keys(languagesByExtension);
84
90
  }
85
91
  //# sourceMappingURL=parsers.js.map
@@ -0,0 +1,10 @@
1
+ import { HighlightStyle as CodeMirrorHighlightStyle } from "@codemirror/language";
2
+ import { type Tag } from "@lezer/highlight";
3
+ import type { HighlightStyle } from "./types.js";
4
+ export declare const syntaxRoleTags: ReadonlyArray<{
5
+ tag: Tag;
6
+ role: HighlightStyle;
7
+ }>;
8
+ export declare const staticSyntaxHighlighter: import("@lezer/highlight").Highlighter;
9
+ export declare function createCodeMirrorHighlightStyle(colors: Record<HighlightStyle, string>): CodeMirrorHighlightStyle;
10
+ //# sourceMappingURL=syntax-roles.d.ts.map
@@ -0,0 +1,50 @@
1
+ import { HighlightStyle as CodeMirrorHighlightStyle } from "@codemirror/language";
2
+ import { tagHighlighter, tags } from "@lezer/highlight";
3
+ export const syntaxRoleTags = [
4
+ { tag: tags.keyword, role: "keyword" },
5
+ { tag: tags.controlKeyword, role: "keyword" },
6
+ { tag: tags.operatorKeyword, role: "keyword" },
7
+ { tag: tags.definitionKeyword, role: "keyword" },
8
+ { tag: tags.moduleKeyword, role: "keyword" },
9
+ { tag: tags.comment, role: "comment" },
10
+ { tag: tags.lineComment, role: "comment" },
11
+ { tag: tags.blockComment, role: "comment" },
12
+ { tag: tags.docComment, role: "comment" },
13
+ { tag: tags.string, role: "string" },
14
+ { tag: tags.special(tags.string), role: "string" },
15
+ { tag: tags.number, role: "number" },
16
+ { tag: tags.integer, role: "number" },
17
+ { tag: tags.float, role: "number" },
18
+ { tag: tags.bool, role: "literal" },
19
+ { tag: tags.null, role: "literal" },
20
+ { tag: tags.function(tags.variableName), role: "function" },
21
+ { tag: tags.function(tags.propertyName), role: "function" },
22
+ { tag: tags.definition(tags.variableName), role: "definition" },
23
+ { tag: tags.definition(tags.propertyName), role: "definition" },
24
+ { tag: tags.definition(tags.function(tags.variableName)), role: "definition" },
25
+ { tag: tags.className, role: "class" },
26
+ { tag: tags.definition(tags.className), role: "class" },
27
+ { tag: tags.typeName, role: "type" },
28
+ { tag: tags.tagName, role: "tag" },
29
+ { tag: tags.attributeName, role: "attribute" },
30
+ { tag: tags.attributeValue, role: "string" },
31
+ { tag: tags.propertyName, role: "property" },
32
+ { tag: tags.variableName, role: "variable" },
33
+ { tag: tags.local(tags.variableName), role: "variable" },
34
+ { tag: tags.special(tags.variableName), role: "variable" },
35
+ { tag: tags.operator, role: "operator" },
36
+ { tag: tags.punctuation, role: "punctuation" },
37
+ { tag: tags.bracket, role: "punctuation" },
38
+ { tag: tags.separator, role: "punctuation" },
39
+ { tag: tags.regexp, role: "regexp" },
40
+ { tag: tags.escape, role: "escape" },
41
+ { tag: tags.meta, role: "meta" },
42
+ { tag: tags.heading, role: "heading" },
43
+ { tag: tags.link, role: "link" },
44
+ { tag: tags.url, role: "link" },
45
+ ];
46
+ export const staticSyntaxHighlighter = tagHighlighter(syntaxRoleTags.map(({ tag, role }) => ({ tag, class: role })));
47
+ export function createCodeMirrorHighlightStyle(colors) {
48
+ return CodeMirrorHighlightStyle.define(syntaxRoleTags.map(({ tag, role }) => ({ tag, color: colors[role] })));
49
+ }
50
+ //# sourceMappingURL=syntax-roles.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getpaseo/highlight",
3
- "version": "0.2.0-beta.1",
3
+ "version": "0.2.0-beta.3",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.map"
@@ -27,7 +27,7 @@
27
27
  "typecheck": "tsgo --noEmit"
28
28
  },
29
29
  "dependencies": {
30
- "@codemirror/language": "^6.12.3",
30
+ "@codemirror/language": "6.12.4",
31
31
  "@codemirror/legacy-modes": "^6.5.3",
32
32
  "@lezer/common": "^1.5.0",
33
33
  "@lezer/cpp": "^1.1.5",