mirror of
https://github.com/stablyai/orca.git
synced 2026-09-30 00:03:15 +00:00
feat(editor): highlight Typst files and diffs (#23323)
Highlight Typst files and diffs through the existing lazy TextMate provider. Based on contributor proposal #22884. Co-authored-by: Quan Nguyen <qnguyen.dev2@gmail.com>
This commit is contained in:
@@ -20,6 +20,22 @@ describe('detectLanguage', () => {
|
||||
expect(detectLanguage('packages/app.nimble')).toBe('nim')
|
||||
})
|
||||
|
||||
it.each([
|
||||
'documents/report.typ',
|
||||
'C:\\documents\\REPORT.TYP',
|
||||
'\\\\server\\share\\report.typ',
|
||||
'/home/user/folder workspace/Report.TyP'
|
||||
])('maps Typst source %s to the typst language id', (filePath) => {
|
||||
expect(detectLanguage(filePath)).toBe('typst')
|
||||
})
|
||||
|
||||
it.each(['report.typ.bak', 'report.typx', 'documents.typ/README', 'documents.typ\\README'])(
|
||||
'keeps non-Typst file %s on plaintext',
|
||||
(filePath) => {
|
||||
expect(detectLanguage(filePath)).toBe('plaintext')
|
||||
}
|
||||
)
|
||||
|
||||
it('maps exact filenames from Windows paths', () => {
|
||||
expect(detectLanguage('C:\\Users\\alice\\repo\\Dockerfile')).toBe('dockerfile')
|
||||
expect(detectLanguage('C:\\Users\\alice\\repo\\CMakeLists.txt')).toBe('cmake')
|
||||
|
||||
@@ -102,6 +102,7 @@ const EXT_TO_LANGUAGE: Record<string, string> = {
|
||||
'.nim': 'nim',
|
||||
'.nims': 'nim',
|
||||
'.nimble': 'nim',
|
||||
'.typ': 'typst',
|
||||
'.tf': 'hcl',
|
||||
'.hcl': 'hcl',
|
||||
'.abap': 'abap',
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
TYPST_LANGUAGE_ID,
|
||||
TYPST_TEXTMATE_SCOPE,
|
||||
loadTypstTextMateGrammar,
|
||||
registerTypstLanguage
|
||||
} from './register-typst'
|
||||
|
||||
function createMonacoMock(languages: { id: string }[] = []) {
|
||||
return {
|
||||
languages: {
|
||||
getLanguages: vi.fn(() => languages),
|
||||
register: vi.fn((language: { id: string }) => languages.push(language)),
|
||||
setLanguageConfiguration: vi.fn(),
|
||||
registerTokensProviderFactory: vi.fn()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe('registerTypstLanguage', () => {
|
||||
it('registers Typst with its extension, TextMate scope, and editor configuration', () => {
|
||||
const monaco = createMonacoMock()
|
||||
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the mock exercises only the four languages.* calls this registration touches; the full Monaco module type cannot be satisfied structurally.
|
||||
registerTypstLanguage(monaco as never)
|
||||
|
||||
expect(monaco.languages.register).toHaveBeenCalledWith({
|
||||
id: TYPST_LANGUAGE_ID,
|
||||
extensions: ['.typ'],
|
||||
aliases: ['Typst', 'typst']
|
||||
})
|
||||
expect(monaco.languages.setLanguageConfiguration).toHaveBeenCalledWith(
|
||||
TYPST_LANGUAGE_ID,
|
||||
expect.objectContaining({
|
||||
comments: { lineComment: '//', blockComment: ['/*', '*/'] },
|
||||
brackets: [
|
||||
['{', '}'],
|
||||
['[', ']'],
|
||||
['(', ')']
|
||||
]
|
||||
})
|
||||
)
|
||||
expect(monaco.languages.registerTokensProviderFactory).toHaveBeenCalledWith(
|
||||
TYPST_LANGUAGE_ID,
|
||||
expect.objectContaining({ create: expect.any(Function) })
|
||||
)
|
||||
})
|
||||
|
||||
it('does not register Typst twice', () => {
|
||||
const monaco = createMonacoMock()
|
||||
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only the mocked languages API is used.
|
||||
registerTypstLanguage(monaco as never)
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only the mocked languages API is used.
|
||||
registerTypstLanguage(monaco as never)
|
||||
|
||||
expect(monaco.languages.register).toHaveBeenCalledTimes(1)
|
||||
expect(monaco.languages.setLanguageConfiguration).toHaveBeenCalledTimes(1)
|
||||
expect(monaco.languages.registerTokensProviderFactory).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('loadTypstTextMateGrammar', () => {
|
||||
it('lazily loads the vendored Typst grammar for the Typst scope', async () => {
|
||||
const grammar = await loadTypstTextMateGrammar(TYPST_TEXTMATE_SCOPE)
|
||||
|
||||
expect(grammar).toMatchObject({
|
||||
name: 'typst',
|
||||
scopeName: TYPST_TEXTMATE_SCOPE
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores unrelated TextMate scopes', async () => {
|
||||
await expect(loadTypstTextMateGrammar('source.python')).resolves.toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,56 @@
|
||||
import type * as Monaco from 'monaco-editor'
|
||||
import type { IRawGrammar } from 'vscode-textmate'
|
||||
import { registerTextMateLanguage } from './textmate-language-registration'
|
||||
|
||||
type MonacoModule = typeof Monaco
|
||||
|
||||
export const TYPST_LANGUAGE_ID = 'typst'
|
||||
export const TYPST_TEXTMATE_SCOPE = 'source.typst'
|
||||
|
||||
export const typstLanguageConfiguration: Monaco.languages.LanguageConfiguration = {
|
||||
comments: {
|
||||
lineComment: '//',
|
||||
blockComment: ['/*', '*/']
|
||||
},
|
||||
brackets: [
|
||||
['{', '}'],
|
||||
['[', ']'],
|
||||
['(', ')']
|
||||
],
|
||||
autoClosingPairs: [
|
||||
{ open: '{', close: '}' },
|
||||
{ open: '[', close: ']' },
|
||||
{ open: '(', close: ')' },
|
||||
{ open: '"', close: '"' }
|
||||
],
|
||||
surroundingPairs: [
|
||||
{ open: '{', close: '}' },
|
||||
{ open: '[', close: ']' },
|
||||
{ open: '(', close: ')' },
|
||||
{ open: '"', close: '"' }
|
||||
]
|
||||
}
|
||||
|
||||
export async function loadTypstTextMateGrammar(scopeName: string): Promise<IRawGrammar | null> {
|
||||
if (scopeName !== TYPST_TEXTMATE_SCOPE) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Lazy upstream grammar; provenance and Apache-2.0 license are in typst-LICENSE.txt.
|
||||
const grammarModule = await import('./textmate-grammars/typst.tmLanguage.json')
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: TextMate injects the $self/$base repository entries required by IRawGrammar; real tokenization tests validate this vendored JSON.
|
||||
return grammarModule.default as unknown as IRawGrammar
|
||||
}
|
||||
|
||||
export function registerTypstLanguage(monaco: MonacoModule): void {
|
||||
registerTextMateLanguage(monaco, {
|
||||
language: {
|
||||
id: TYPST_LANGUAGE_ID,
|
||||
extensions: ['.typ'],
|
||||
aliases: ['Typst', 'typst']
|
||||
},
|
||||
configuration: typstLanguageConfiguration,
|
||||
scopeName: TYPST_TEXTMATE_SCOPE,
|
||||
loadGrammar: loadTypstTextMateGrammar
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
Typst
|
||||
|
||||
Source: https://github.com/typst/typst/blob/9f2b6e8715237cb086899a42873660fe744622e8/tools/support/typst.tmLanguage.json
|
||||
Modifications by Orca: JSON formatting; line comments have opaque bodies;
|
||||
block comments nest only block comments; math reuses string and comment rules
|
||||
and consumes escapes before recognizing its closing dollar delimiter.
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
@@ -0,0 +1,383 @@
|
||||
{
|
||||
"comment": "Adapted by Orca: isolate comment nesting and protect strings, escapes, and comments inside math. See typst-LICENSE.txt for pinned upstream provenance.",
|
||||
"name": "typst",
|
||||
"patterns": [{ "include": "#markup" }],
|
||||
"repository": {
|
||||
"comments": {
|
||||
"patterns": [
|
||||
{ "include": "#block-comment" },
|
||||
{
|
||||
"name": "comment.line.double-slash.typst",
|
||||
"begin": "(?<!:)//",
|
||||
"end": "\n",
|
||||
"beginCaptures": { "0": { "name": "punctuation.definition.comment.typst" } }
|
||||
}
|
||||
]
|
||||
},
|
||||
"common": {
|
||||
"patterns": [{ "include": "#comments" }]
|
||||
},
|
||||
"markup": {
|
||||
"patterns": [
|
||||
{ "include": "#common" },
|
||||
{
|
||||
"name": "constant.character.escape.content.typst",
|
||||
"match": "\\\\([\\\\/\\[\\]{}#*_=~`$-.]|u\\{[0-9a-zA-Z]*\\}?)"
|
||||
},
|
||||
{
|
||||
"name": "punctuation.definition.linebreak.typst",
|
||||
"match": "\\\\"
|
||||
},
|
||||
{
|
||||
"name": "punctuation.definition.nonbreaking-space.typst",
|
||||
"match": "~"
|
||||
},
|
||||
{
|
||||
"name": "punctuation.definition.shy.typst",
|
||||
"match": "-\\?"
|
||||
},
|
||||
{
|
||||
"name": "punctuation.definition.em-dash.typst",
|
||||
"match": "---"
|
||||
},
|
||||
{
|
||||
"name": "punctuation.definition.en-dash.typst",
|
||||
"match": "--"
|
||||
},
|
||||
{
|
||||
"name": "punctuation.definition.ellipsis.typst",
|
||||
"match": "\\.\\.\\."
|
||||
},
|
||||
{
|
||||
"name": "constant.symbol.typst",
|
||||
"match": ":([a-zA-Z0-9]+:)+"
|
||||
},
|
||||
{
|
||||
"name": "markup.bold.typst",
|
||||
"begin": "(^\\*|\\*$|((?<=\\W|_)\\*)|(\\*(?=\\W|_)))",
|
||||
"end": "(^\\*|\\*$|((?<=\\W|_)\\*)|(\\*(?=\\W|_)))|\n|(?=\\])",
|
||||
"captures": { "0": { "name": "punctuation.definition.bold.typst" } },
|
||||
"patterns": [{ "include": "#markup" }]
|
||||
},
|
||||
{
|
||||
"name": "markup.italic.typst",
|
||||
"begin": "(^_|_$|((?<=\\W|_)_)|(_(?=\\W|_)))",
|
||||
"end": "(^_|_$|((?<=\\W|_)_)|(_(?=\\W|_)))|\n|(?=\\])",
|
||||
"captures": { "0": { "name": "punctuation.definition.italic.typst" } },
|
||||
"patterns": [{ "include": "#markup" }]
|
||||
},
|
||||
{
|
||||
"name": "markup.underline.link.typst",
|
||||
"match": "https?://[0-9a-zA-Z~/%#&=',;\\.\\+\\?]*"
|
||||
},
|
||||
{
|
||||
"name": "markup.raw.block.typst",
|
||||
"begin": "`{3,}",
|
||||
"end": "\\0",
|
||||
"captures": { "0": { "name": "punctuation.definition.raw.typst" } }
|
||||
},
|
||||
{
|
||||
"name": "markup.raw.inline.typst",
|
||||
"begin": "`",
|
||||
"end": "`",
|
||||
"captures": { "0": { "name": "punctuation.definition.raw.typst" } }
|
||||
},
|
||||
{ "include": "#math" },
|
||||
{
|
||||
"name": "markup.heading.typst",
|
||||
"contentName": "entity.name.section.typst",
|
||||
"begin": "^\\s*=+\\s+",
|
||||
"end": "\n|(?=<)",
|
||||
"beginCaptures": { "0": { "name": "punctuation.definition.heading.typst" } },
|
||||
"patterns": [{ "include": "#markup" }]
|
||||
},
|
||||
{
|
||||
"name": "punctuation.definition.list.unnumbered.typst",
|
||||
"match": "^\\s*-\\s+"
|
||||
},
|
||||
{
|
||||
"name": "punctuation.definition.list.numbered.typst",
|
||||
"match": "^\\s*([0-9]*\\.|\\+)\\s+"
|
||||
},
|
||||
{
|
||||
"match": "^\\s*(/)\\s+([^:]*:)",
|
||||
"captures": {
|
||||
"1": { "name": "punctuation.definition.list.description.typst" },
|
||||
"2": { "name": "markup.list.term.typst" }
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "entity.other.label.typst",
|
||||
"match": "<[[:alpha:]_][[:alnum:]_-]*>",
|
||||
"captures": { "1": { "name": "punctuation.definition.label.typst" } }
|
||||
},
|
||||
{
|
||||
"name": "entity.other.reference.typst",
|
||||
"match": "(@)[[:alpha:]_][[:alnum:]_-]*",
|
||||
"captures": { "1": { "name": "punctuation.definition.reference.typst" } }
|
||||
},
|
||||
{
|
||||
"begin": "(#)(let|set|show|context)\\b",
|
||||
"end": "\n|(;)|(?=])",
|
||||
"beginCaptures": {
|
||||
"0": { "name": "keyword.other.typst" },
|
||||
"1": { "name": "punctuation.definition.keyword.typst" }
|
||||
},
|
||||
"endCaptures": { "1": { "name": "punctuation.terminator.statement.typst" } },
|
||||
"patterns": [{ "include": "#code" }]
|
||||
},
|
||||
{
|
||||
"name": "keyword.other.typst",
|
||||
"match": "(#)(as|in)\\b",
|
||||
"captures": { "1": { "name": "punctuation.definition.keyword.typst" } }
|
||||
},
|
||||
{
|
||||
"begin": "((#)if|(?<=(}|])\\s*)else)\\b",
|
||||
"end": "\n|(?=])|(?<=}|])",
|
||||
"beginCaptures": {
|
||||
"0": { "name": "keyword.control.conditional.typst" },
|
||||
"2": { "name": "punctuation.definition.keyword.typst" }
|
||||
},
|
||||
"patterns": [{ "include": "#code" }]
|
||||
},
|
||||
{
|
||||
"begin": "(#)(for|while)\\b",
|
||||
"end": "\n|(?=])|(?<=}|])",
|
||||
"beginCaptures": {
|
||||
"0": { "name": "keyword.control.loop.typst" },
|
||||
"1": { "name": "punctuation.definition.keyword.typst" }
|
||||
},
|
||||
"patterns": [{ "include": "#code" }]
|
||||
},
|
||||
{
|
||||
"name": "keyword.control.loop.typst",
|
||||
"match": "(#)(break|continue)\\b",
|
||||
"captures": { "1": { "name": "punctuation.definition.keyword.typst" } }
|
||||
},
|
||||
{
|
||||
"begin": "(#)(import|include|export)\\b",
|
||||
"end": "\n|(;)|(?=])",
|
||||
"beginCaptures": {
|
||||
"0": { "name": "keyword.control.import.typst" },
|
||||
"1": { "name": "punctuation.definition.keyword.typst" }
|
||||
},
|
||||
"endCaptures": { "1": { "name": "punctuation.terminator.statement.typst" } },
|
||||
"patterns": [{ "include": "#code" }]
|
||||
},
|
||||
{
|
||||
"name": "keyword.control.flow.typst",
|
||||
"match": "(#)(return)\\b",
|
||||
"captures": { "1": { "name": "punctuation.definition.keyword.typst" } }
|
||||
},
|
||||
{
|
||||
"comment": "Function name",
|
||||
"name": "entity.name.function.typst",
|
||||
"match": "((#)[[:alpha:]_][[:alnum:]_-]*!?)(?=\\[|\\()",
|
||||
"captures": { "2": { "name": "punctuation.definition.function.typst" } }
|
||||
},
|
||||
{
|
||||
"comment": "Function arguments",
|
||||
"begin": "(?<=#[[:alpha:]_][[:alnum:]_-]*!?)\\(",
|
||||
"end": "\\)",
|
||||
"captures": { "0": { "name": "punctuation.definition.group.typst" } },
|
||||
"patterns": [{ "include": "#arguments" }]
|
||||
},
|
||||
{
|
||||
"name": "entity.other.interpolated.typst",
|
||||
"match": "(#)[[:alpha:]_][.[:alnum:]_-]*",
|
||||
"captures": { "1": { "name": "punctuation.definition.variable.typst" } }
|
||||
},
|
||||
{
|
||||
"name": "meta.block.content.typst",
|
||||
"begin": "#",
|
||||
"end": "\\s",
|
||||
"patterns": [{ "include": "#code" }]
|
||||
}
|
||||
]
|
||||
},
|
||||
"code": {
|
||||
"patterns": [
|
||||
{ "include": "#common" },
|
||||
{
|
||||
"name": "meta.block.code.typst",
|
||||
"begin": "{",
|
||||
"end": "}",
|
||||
"captures": { "0": { "name": "punctuation.definition.block.code.typst" } },
|
||||
"patterns": [{ "include": "#code" }]
|
||||
},
|
||||
{
|
||||
"name": "meta.block.content.typst",
|
||||
"begin": "\\[",
|
||||
"end": "\\]",
|
||||
"captures": { "0": { "name": "punctuation.definition.block.content.typst" } },
|
||||
"patterns": [{ "include": "#markup" }]
|
||||
},
|
||||
{
|
||||
"name": "comment.line.double-slash.typst",
|
||||
"begin": "//",
|
||||
"end": "\n",
|
||||
"beginCaptures": { "0": { "name": "punctuation.definition.comment.typst" } }
|
||||
},
|
||||
{
|
||||
"name": "punctuation.separator.colon.typst",
|
||||
"match": ":"
|
||||
},
|
||||
{
|
||||
"name": "punctuation.separator.comma.typst",
|
||||
"match": ","
|
||||
},
|
||||
{
|
||||
"name": "keyword.operator.typst",
|
||||
"match": "=>|\\.\\."
|
||||
},
|
||||
{
|
||||
"name": "keyword.operator.relational.typst",
|
||||
"match": "==|!=|<=|<|>=|>"
|
||||
},
|
||||
{
|
||||
"name": "keyword.operator.assignment.typst",
|
||||
"match": "\\+=|-=|\\*=|/=|="
|
||||
},
|
||||
{
|
||||
"name": "keyword.operator.arithmetic.typst",
|
||||
"match": "\\+|\\*|/|(?<![[:alpha:]_][[:alnum:]_-]*)-(?![:alnum:]_-]*[[:alpha:]_])"
|
||||
},
|
||||
{
|
||||
"name": "keyword.operator.word.typst",
|
||||
"match": "\\b(and|or|not)\\b"
|
||||
},
|
||||
{
|
||||
"name": "keyword.other.typst",
|
||||
"match": "\\b(let|as|in|set|show|context)\\b"
|
||||
},
|
||||
{
|
||||
"name": "keyword.control.conditional.typst",
|
||||
"match": "\\b(if|else)\\b"
|
||||
},
|
||||
{
|
||||
"name": "keyword.control.loop.typst",
|
||||
"match": "\\b(for|while|break|continue)\\b"
|
||||
},
|
||||
{
|
||||
"name": "keyword.control.import.typst",
|
||||
"match": "\\b(import|include|export)\\b"
|
||||
},
|
||||
{
|
||||
"name": "keyword.control.flow.typst",
|
||||
"match": "\\b(return)\\b"
|
||||
},
|
||||
{ "include": "#constants" },
|
||||
{
|
||||
"comment": "Function name",
|
||||
"name": "entity.name.function.typst",
|
||||
"match": "\\b[[:alpha:]_][[:alnum:]_-]*!?(?=\\[|\\()"
|
||||
},
|
||||
{
|
||||
"comment": "Function name",
|
||||
"name": "entity.name.function.typst",
|
||||
"match": "(?<=\\bshow\\s*)\\b[[:alpha:]_][[:alnum:]_-]*(?=\\s*[:.])"
|
||||
},
|
||||
{
|
||||
"comment": "Function arguments",
|
||||
"begin": "(?<=\\b[[:alpha:]_][[:alnum:]_-]*!?)\\(",
|
||||
"end": "\\)",
|
||||
"captures": { "0": { "name": "punctuation.definition.group.typst" } },
|
||||
"patterns": [{ "include": "#arguments" }]
|
||||
},
|
||||
{
|
||||
"name": "variable.other.typst",
|
||||
"match": "\\b[[:alpha:]_][[:alnum:]_-]*\\b"
|
||||
},
|
||||
{
|
||||
"name": "meta.group.typst",
|
||||
"begin": "\\(",
|
||||
"end": "\\)|(?=;)",
|
||||
"captures": { "0": { "name": "punctuation.definition.group.typst" } },
|
||||
"patterns": [{ "include": "#code" }]
|
||||
}
|
||||
]
|
||||
},
|
||||
"constants": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "constant.language.none.typst",
|
||||
"match": "\\bnone\\b"
|
||||
},
|
||||
{
|
||||
"name": "constant.language.auto.typst",
|
||||
"match": "\\bauto\\b"
|
||||
},
|
||||
{
|
||||
"name": "constant.language.boolean.typst",
|
||||
"match": "\\b(true|false)\\b"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.length.typst",
|
||||
"match": "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?(mm|pt|cm|in|em)\\b"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.angle.typst",
|
||||
"match": "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?(rad|deg)\\b"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.percentage.typst",
|
||||
"match": "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?%"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.fr.typst",
|
||||
"match": "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?fr"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.integer.typst",
|
||||
"match": "\\b(0x[0-9a-zA-Z]+|(0b|0o)?\\d+)\\b"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.float.typst",
|
||||
"match": "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?\\b"
|
||||
},
|
||||
{ "include": "#string" },
|
||||
{ "include": "#math" }
|
||||
]
|
||||
},
|
||||
"arguments": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "variable.parameter.typst",
|
||||
"match": "\\b[[:alpha:]_][[:alnum:]_-]*(?=:)"
|
||||
},
|
||||
{ "include": "#code" }
|
||||
]
|
||||
},
|
||||
"block-comment": {
|
||||
"name": "comment.block.typst",
|
||||
"begin": "/\\*",
|
||||
"end": "\\*/",
|
||||
"captures": { "0": { "name": "punctuation.definition.comment.typst" } },
|
||||
"patterns": [{ "include": "#block-comment" }]
|
||||
},
|
||||
"string": {
|
||||
"name": "string.quoted.double.typst",
|
||||
"begin": "\"",
|
||||
"end": "\"",
|
||||
"captures": { "0": { "name": "punctuation.definition.string.typst" } },
|
||||
"patterns": [
|
||||
{
|
||||
"name": "constant.character.escape.string.typst",
|
||||
"match": "\\\\([\\\\\"nrt]|u\\{?[0-9a-zA-Z]*\\}?)"
|
||||
}
|
||||
]
|
||||
},
|
||||
"math": {
|
||||
"name": "string.other.math.typst",
|
||||
"begin": "\\$",
|
||||
"end": "\\$",
|
||||
"captures": { "0": { "name": "punctuation.definition.string.math.typst" } },
|
||||
"patterns": [
|
||||
{ "include": "#comments" },
|
||||
{ "include": "#string" },
|
||||
{ "name": "constant.character.escape.math.typst", "match": "\\\\." }
|
||||
]
|
||||
}
|
||||
},
|
||||
"scopeName": "source.typst"
|
||||
}
|
||||
@@ -65,8 +65,12 @@ describe('registerTextMateLanguage', () => {
|
||||
expect(loadProviderModule).not.toHaveBeenCalled()
|
||||
|
||||
const providerPromise = createTokensProvider()
|
||||
expect(createTokensProvider()).toBe(providerPromise)
|
||||
|
||||
await expect(providerPromise).resolves.toBe(provider)
|
||||
expect(createTokensProvider()).toBe(providerPromise)
|
||||
expect(loadProviderModule).toHaveBeenCalledTimes(1)
|
||||
expect(createTextMateTokensProvider).toHaveBeenCalledTimes(1)
|
||||
expect(createTextMateTokensProvider).toHaveBeenCalledWith({
|
||||
scopeName: 'source.nim',
|
||||
loadGrammar
|
||||
|
||||
@@ -4,6 +4,7 @@ import { describe, expect, it } from 'vitest'
|
||||
import { createOnigScanner, createOnigString, loadWASM } from 'vscode-oniguruma'
|
||||
import type { IOnigLib, IRawGrammar } from 'vscode-textmate'
|
||||
import nimGrammar from './textmate-grammars/nim.tmLanguage.json'
|
||||
import { loadTypstTextMateGrammar } from './register-typst'
|
||||
import { createTextMateTokensProvider } from './textmate-token-provider'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
@@ -46,6 +47,107 @@ describe('createTextMateTokensProvider', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('tokenizes Typst markup, code, and math through the lazy grammar loader', async () => {
|
||||
const provider = await createTextMateTokensProvider({
|
||||
scopeName: 'source.typst',
|
||||
loadGrammar: loadTypstTextMateGrammar,
|
||||
loadOniguruma: loadNodeOniguruma
|
||||
})
|
||||
const scopesOf = (line: string) =>
|
||||
provider.tokenize(line, provider.getInitialState()).tokens.map((token) => token.scopes)
|
||||
|
||||
expect(scopesOf('#let width = 12pt')).toEqual(
|
||||
expect.arrayContaining(['keyword.other.typst', 'constant.numeric.length.typst'])
|
||||
)
|
||||
expect(scopesOf('#set text(font: "Inter")')).toEqual(
|
||||
expect.arrayContaining(['entity.name.function.typst', 'string.quoted.double.typst'])
|
||||
)
|
||||
expect(scopesOf('// note')).toContain('comment.line.double-slash.typst')
|
||||
expect(scopesOf('$ sum_(k=0)^n k $')).toContain('string.other.math.typst')
|
||||
})
|
||||
|
||||
it.each([
|
||||
['```rust', 'let x = 1', '```', '#let y = 2'],
|
||||
['````', '```', '````', '#let y = 2']
|
||||
])('carries a Typst raw block until its matching fence closes (%s)', async (...lines) => {
|
||||
const provider = await createTextMateTokensProvider({
|
||||
scopeName: 'source.typst',
|
||||
loadGrammar: loadTypstTextMateGrammar,
|
||||
loadOniguruma: loadNodeOniguruma
|
||||
})
|
||||
|
||||
let state = provider.getInitialState()
|
||||
const lineScopes = lines.map((line) => {
|
||||
const result = provider.tokenize(line, state)
|
||||
state = result.endState
|
||||
return result.tokens.map((token) => token.scopes)
|
||||
})
|
||||
|
||||
expect(lineScopes[1]).toEqual(['markup.raw.block.typst'])
|
||||
expect(lineScopes[3]).toContain('keyword.other.typst')
|
||||
})
|
||||
|
||||
it('carries Typst math across lines and returns to code after the closing dollar', async () => {
|
||||
const provider = await createTextMateTokensProvider({
|
||||
scopeName: 'source.typst',
|
||||
loadGrammar: loadTypstTextMateGrammar,
|
||||
loadOniguruma: loadNodeOniguruma
|
||||
})
|
||||
const opening = provider.tokenize('$', provider.getInitialState())
|
||||
const clone = opening.endState.clone()
|
||||
expect(clone.equals(opening.endState)).toBe(true)
|
||||
const body = provider.tokenize('x^2 + y^2 = z^2', clone)
|
||||
expect(body.tokens.map((token) => token.scopes)).toEqual(['string.other.math.typst'])
|
||||
const closing = provider.tokenize('$', body.endState)
|
||||
const after = provider.tokenize('#let y = 2', closing.endState)
|
||||
expect(after.tokens.map((token) => token.scopes)).toContain('keyword.other.typst')
|
||||
expect(after.tokens.map((token) => token.scopes)).not.toContain('string.other.math.typst')
|
||||
expect(provider.tokenize('#let y = 2', provider.getInitialState()).tokens).toEqual(after.tokens)
|
||||
})
|
||||
|
||||
it.each([
|
||||
['// Write /* to start a block comment.'],
|
||||
['/* documentation: // */'],
|
||||
['/* outer', '/* inner */ still outer', '*/'],
|
||||
['$ "cost $5" + x $'],
|
||||
['$ x + \\$ + y $'],
|
||||
['$ "escaped \\" $5" + x $'],
|
||||
['$ x + \\\\ $'],
|
||||
['$', '"cost $5" + \\$ + y', '$'],
|
||||
['$ x /* $ ignored */ + y $'],
|
||||
['$', '// $ ignored', 'x + y', '$'],
|
||||
['#let formula = $ "cost $5" + \\$ $']
|
||||
])('returns to Typst code after a comment or math region (%j)', async (...lines) => {
|
||||
const provider = await createTextMateTokensProvider({
|
||||
scopeName: 'source.typst',
|
||||
loadGrammar: loadTypstTextMateGrammar,
|
||||
loadOniguruma: loadNodeOniguruma
|
||||
})
|
||||
let state = provider.getInitialState()
|
||||
for (const line of lines) {
|
||||
state = provider.tokenize(line, state).endState
|
||||
}
|
||||
const after = '#let after = 12pt'
|
||||
const tokens = provider.tokenize(after, state).tokens
|
||||
expect(tokens).toEqual(provider.tokenize(after, provider.getInitialState()).tokens)
|
||||
expect(tokens.map((token) => token.scopes)).toEqual(
|
||||
expect.arrayContaining(['keyword.other.typst', 'constant.numeric.length.typst'])
|
||||
)
|
||||
})
|
||||
|
||||
it('keeps nested Typst block comments active until both closers', async () => {
|
||||
const provider = await createTextMateTokensProvider({
|
||||
scopeName: 'source.typst',
|
||||
loadGrammar: loadTypstTextMateGrammar,
|
||||
loadOniguruma: loadNodeOniguruma
|
||||
})
|
||||
let state = provider.tokenize('/* outer /* inner', provider.getInitialState()).endState
|
||||
state = provider.tokenize('*/ still outer', state).endState
|
||||
expect(provider.tokenize('#let hidden = 2', state).tokens.map((token) => token.scopes)).toEqual(
|
||||
['comment.block.typst']
|
||||
)
|
||||
})
|
||||
|
||||
it('fails clearly when a scope has no grammar', async () => {
|
||||
await expect(
|
||||
createTextMateTokensProvider({
|
||||
|
||||
@@ -13,6 +13,7 @@ import { registerJsonlLanguage } from './monaco-languages/register-jsonl'
|
||||
import { registerNimLanguage } from './monaco-languages/register-nim'
|
||||
import { registerShellMarkdownAliases } from './monaco-languages/register-shell-markdown-aliases'
|
||||
import { registerSvelteLanguage } from './monaco-languages/register-svelte'
|
||||
import { registerTypstLanguage } from './monaco-languages/register-typst'
|
||||
import { registerVueLanguage } from './monaco-languages/register-vue'
|
||||
import { installMonacoDelayerCancellationGuard } from './monaco-delayer-cancellation-guard'
|
||||
import { installMonacoDiffEditorDisposalGuard } from './monaco-diff-editor-disposal'
|
||||
@@ -82,6 +83,7 @@ runMonacoSetupSteps([
|
||||
['Svelte language registration', () => registerSvelteLanguage(monaco)],
|
||||
['Astro language registration', () => registerAstroLanguage(monaco)],
|
||||
['Nim language registration', () => registerNimLanguage(monaco)],
|
||||
['Typst language registration', () => registerTypstLanguage(monaco)],
|
||||
['JSONL language registration', () => registerJsonlLanguage(monaco)],
|
||||
['shell Markdown alias registration', () => registerShellMarkdownAliases(monaco)],
|
||||
['delayer cancellation guard', installMonacoDelayerCancellationGuard],
|
||||
|
||||
Reference in New Issue
Block a user