Skip to content

Commit

Permalink
Move the base CodeMirror KCL support to a local package (#4897)
Browse files Browse the repository at this point in the history
* Move CodeMirror LRLanguage to new file

This separates the base language support from the LSP and color picker.

* Move the base CodeMirror KCL support to a local package

* Start CodeMirror grammar tests

* Exclude vitest config in tsconfig

* Add KCL path to tsconfig

* Remove stray import

* Drop extension from import

* Use __filename for commonjs compat

* Check exec return before access

* Build ES and CJS to dist

* Format

* Exclude all.test.ts from codespell

This is to work around "fileTests" imported from Lezer. Future codespell versions look
like they'll allow the code to be annotated, which would be nicer.

---------

Co-authored-by: Matt Mundell <[email protected]>
  • Loading branch information
jtran and mattmundell authored Jan 4, 2025
1 parent 474acb1 commit 3c53bab
Show file tree
Hide file tree
Showing 15 changed files with 962 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .codespellrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[codespell]
ignore-words-list: crate,everytime,inout,co-ordinate,ot,nwo,absolutey,atleast,ue,afterall
skip: **/target,node_modules,build,**/Cargo.lock,./docs/kcl/*.md,.yarn.lock,**/yarn.lock,./openapi/*.json,./src/lib/machine-api.d.ts
ignore-words-list: crate,everytime,inout,co-ordinate,ot,nwo,atleast,ue,afterall
skip: **/target,node_modules,build,**/Cargo.lock,./docs/kcl/*.md,.yarn.lock,**/yarn.lock,./openapi/*.json,./src/lib/machine-api.d.ts,./packages/codemirror-lang-kcl/test/all.test.ts
7 changes: 7 additions & 0 deletions packages/codemirror-lang-kcl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
build
dist
tsconfig.tsbuildinfo
*.d.ts
*.js
!rollup.config.js
36 changes: 36 additions & 0 deletions packages/codemirror-lang-kcl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@kittycad/codemirror-lang-kcl",
"version": "1.0.0",
"description": "Zoo KCL language support for CodeMirror 6.",
"main": "src/index.ts",
"scripts": {
"build": "rollup -c",
"test": "vitest --config vitest.main.config.ts run"
},
"type": "module",
"repository": "https://github.com/KittyCAD/modeling-app",
"author": "Zoo Engineering Team",
"license": "MIT",
"private": false,
"exports": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"types": "dist/index.d.ts",
"dependencies": {
"@codemirror/language": "^6.10.3",
"@codemirror/state": "^6.4.1",
"@lezer/highlight": "^1.2.1",
"typescript": "^5.7.2"
},
"devDependencies": {
"@lezer/generator": "^1.7.2",
"@rollup/plugin-typescript": "^12.1.2",
"rollup": "^4.29.1",
"rollup-plugin-dts": "^6.1.1",
"vitest": "^2.1.8"
},
"files": [
"dist/"
]
}
25 changes: 25 additions & 0 deletions packages/codemirror-lang-kcl/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dts from 'rollup-plugin-dts'
import { lezer } from '@lezer/generator/rollup'
import typescript from '@rollup/plugin-typescript'

export default [
{
input: 'src/index.ts',
// imports are considered internal if they start with './' or '/' or 'word:'
external: (id) => id != 'tslib' && !/^(\.?\/|\w:)/.test(id),
output: [
{ file: 'dist/index.cjs', format: 'cjs' },
{ file: 'dist/index.js', format: 'es' },
],
plugins: [lezer(), typescript()],
},
{
input: 'src/index.ts',
external: (id) => id != 'tslib' && !/^(\.?\/|\w:)/.test(id),
output: [
{ file: 'dist/index.d.cts', format: 'cjs' },
{ file: 'dist/index.d.ts', format: 'es' },
],
plugins: [lezer(), typescript(), dts()],
},
]
File renamed without changes.
42 changes: 42 additions & 0 deletions packages/codemirror-lang-kcl/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Base CodeMirror language support for kcl.

import {
LRLanguage,
LanguageSupport,
indentNodeProp,
continuedIndent,
delimitedIndent,
foldNodeProp,
foldInside,
} from '@codemirror/language'
// @ts-ignore: No types available
import { parser } from './kcl.grammar'

export const KclLanguage = LRLanguage.define({
name: 'kcl',
parser: parser.configure({
props: [
indentNodeProp.add({
Body: delimitedIndent({ closing: '}' }),
BlockComment: () => null,
'Statement Property': continuedIndent({ except: /^{/ }),
}),
foldNodeProp.add({
'Body ArrayExpression ObjectExpression': foldInside,
BlockComment(tree) {
return { from: tree.from + 2, to: tree.to - 2 }
},
PipeExpression(tree) {
return { from: tree.firstChild!.to, to: tree.to }
},
}),
],
}),
languageData: {
commentTokens: { line: '//', block: { open: '/*', close: '*/' } },
},
})

export function kcl() {
return new LanguageSupport(KclLanguage)
}
File renamed without changes.
22 changes: 22 additions & 0 deletions packages/codemirror-lang-kcl/test/all.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { KclLanguage } from '../src/index'
import { fileTests } from '@lezer/generator/dist/test'

import * as fs from 'fs'
import * as path from 'path'

let caseDir = path.dirname(__filename)

for (let file of fs.readdirSync(caseDir)) {
if (!/\.txt$/.test(file)) continue

let fname = /^[^\.]*/.exec(file)?.at(0)
if (fname) {
let tests = fileTests(
fs.readFileSync(path.join(caseDir, file), 'utf8'),
file
)
describe(fname, () => {
for (let { name, run } of tests) it(name, () => run(KclLanguage.parser))
})
}
}
60 changes: 60 additions & 0 deletions packages/codemirror-lang-kcl/test/cases.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Booleans

true
false

==>

Program(ExpressionStatement(true), ExpressionStatement(false))

# Identifiers

one
_Two_Three
Four5

==>

Program(ExpressionStatement(VariableName),
ExpressionStatement(VariableName),
ExpressionStatement(VariableName))

# Strings

"hello"
'hi'
"one\"\\two"
'3\'\\four\x'

==>

Program(ExpressionStatement(String),
ExpressionStatement(String),
ExpressionStatement(String),
ExpressionStatement(String))

# VariableDeclaration

let a = 'abc'
export const x = 0.2

==>

Program(VariableDeclaration(let, VariableDefinition, Equals, String),
VariableDeclaration(export, const, VariableDefinition, Equals, Number))

# IfExpression

if x { 1 } else { $tag }

==>

Program(ExpressionStatement(IfExpression(if, VariableName, Body(ExpressionStatement(Number)), else, Body(ExpressionStatement(TagDeclarator)))))

# Shebang

#!anything

==>

Program(Shebang)
19 changes: 19 additions & 0 deletions packages/codemirror-lang-kcl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist",
"target": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"skipLibCheck": true,
"declaration": true
},
"include": ["src", "./*.ts"],
"exclude": ["node_modules", "vitest.main.config.ts"]
}
29 changes: 29 additions & 0 deletions packages/codemirror-lang-kcl/vitest.main.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Overrides the test options from the modeling-app config.

import viteTsconfigPaths from 'vite-tsconfig-paths'
import { defineConfig, configDefaults } from 'vitest/config'
// @ts-ignore: No types available
import { lezer } from '@lezer/generator/rollup'

const config = defineConfig({
test: {
globals: true,
pool: 'forks',
poolOptions: {
forks: {
maxForks: 2,
minForks: 1,
},
},
environment: 'node',
reporters: process.env.GITHUB_ACTIONS
? ['dot', 'github-actions']
: ['verbose', 'hanging-process'],
testTimeout: 1000,
hookTimeout: 1000,
teardownTimeout: 1000,
},
plugins: [viteTsconfigPaths(), lezer()],
})

export default config
Loading

0 comments on commit 3c53bab

Please sign in to comment.