Skip to content

Commit

Permalink
Merge pull request #4 from tidbcloud/chore/extensions-monorepo
Browse files Browse the repository at this point in the history
feat: extensions support monorepo
  • Loading branch information
sanshuiyijing authored Jun 12, 2024
2 parents b05f1bb + ee939c1 commit b7fad88
Show file tree
Hide file tree
Showing 25 changed files with 329 additions and 324 deletions.
File renamed without changes.
5 changes: 4 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"@codemirror/view": "^6.26.3"
},
"dependencies": {
"@tidbcloud/tisqleditor-extensions": "workspace:^"
"@tidbcloud/tisqleditor-extensions-basic-setup": "workspace:^",
"@tidbcloud/tisqleditor-extensions-sql-parser": "workspace:^",
"@tidbcloud/tisqleditor-extensions-cur-sql": "workspace:^",
"@tidbcloud/tisqleditor-extensions-lang-sql": "workspace:^"
}
}
24 changes: 21 additions & 3 deletions packages/core/src/editor-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ import { search } from '@codemirror/search'
import { Compartment, EditorState, Extension } from '@codemirror/state'
import { EditorView } from '@codemirror/view'

import { curSql, getCurStatements, langSql, getNearbyStatement, getSqlStatements, sqlParser, BasicSetupOptions, basicSetup } from '@tidbcloud/tisqleditor-extensions'
import {
BasicSetupOptions,
basicSetup
} from '@tidbcloud/tisqleditor-extensions-basic-setup'
import {
sqlParser,
getSqlStatements,
getNearbyStatement
} from '@tidbcloud/tisqleditor-extensions-sql-parser'
import { langSql } from '@tidbcloud/tisqleditor-extensions-lang-sql'
import {
curSql,
getCurStatements
} from '@tidbcloud/tisqleditor-extensions-cur-sql'

export class SQLEditorInstance {
constructor(
public editor: EditorView,
public themeCompartment: Compartment,
public sqlCompartment: Compartment,
public extraData: {}
) { }
) {}

changeTheme(theme: Extension) {
if (this.themeCompartment.get(this.editor.state) === theme) return
Expand Down Expand Up @@ -87,6 +100,11 @@ export const createSQLEditorInstance = ({
extensions
})
})
const editorInst = new SQLEditorInstance(editor, themeCompartment, sqlCompartment, extraData)
const editorInst = new SQLEditorInstance(
editor,
themeCompartment,
sqlCompartment,
extraData
)
return editorInst
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@tidbcloud/tisqleditor-extensions",
"name": "@tidbcloud/tisqleditor-extensions-basic-setup",
"version": "0.0.1",
"description": "tisqleditor extensions",
"type": "module",
Expand All @@ -20,7 +20,7 @@
},
"keywords": [],
"author": "",
"license": "ISC",
"license": "MIT",
"devDependencies": {
"@codemirror/autocomplete": "^6.16.2",
"@codemirror/commands": "6.3.3",
Expand Down
10 changes: 10 additions & 0 deletions packages/extensions/basic-setup/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typescript from '@rollup/plugin-typescript'

export default {
input: './src/index.ts',
output: {
dir: './dist',
format: 'es'
},
plugins: [typescript()]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// - https://github.com/uiwjs/react-codemirror/blob/master/extensions/basic-setup/src/index.ts
// - https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts

import { autocompletion, completionKeymap, closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete'
import {
autocompletion,
completionKeymap,
closeBrackets,
closeBracketsKeymap
} from '@codemirror/autocomplete'
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands'
import {
defaultHighlightStyle,
Expand Down Expand Up @@ -115,23 +120,31 @@ export const basicSetup = (options: BasicSetupOptions = {}): Extension[] => {

const extensions: Extension[] = []
if (options.lineNumbers !== false) extensions.push(lineNumbers())
if (options.highlightActiveLineGutter !== false) extensions.push(highlightActiveLineGutter())
if (options.highlightSpecialChars !== false) extensions.push(highlightSpecialChars())
if (options.highlightActiveLineGutter !== false)
extensions.push(highlightActiveLineGutter())
if (options.highlightSpecialChars !== false)
extensions.push(highlightSpecialChars())
if (options.history !== false) extensions.push(history())
if (options.foldGutter !== false) extensions.push(foldGutter())
if (options.drawSelection !== false) extensions.push(drawSelection())
if (options.dropCursor !== false) extensions.push(dropCursor())
if (options.allowMultipleSelections !== false) extensions.push(EditorState.allowMultipleSelections.of(true))
if (options.allowMultipleSelections !== false)
extensions.push(EditorState.allowMultipleSelections.of(true))
if (options.indentOnInput !== false) extensions.push(indentOnInput())
if (options.syntaxHighlighting !== false)
extensions.push(syntaxHighlighting(defaultHighlightStyle, { fallback: true }))
extensions.push(
syntaxHighlighting(defaultHighlightStyle, { fallback: true })
)
if (options.bracketMatching !== false) extensions.push(bracketMatching())
if (options.closeBrackets !== false) extensions.push(closeBrackets())
if (options.autocompletion !== false) extensions.push(autocompletion())
if (options.rectangularSelection !== false) extensions.push(rectangularSelection())
if (options.rectangularSelection !== false)
extensions.push(rectangularSelection())
if (initCrosshairCursor !== false) extensions.push(crosshairCursor())
if (options.highlightActiveLine !== false) extensions.push(highlightActiveLine())
if (options.highlightSelectionMatches !== false) extensions.push(highlightSelectionMatches())
if (options.highlightActiveLine !== false)
extensions.push(highlightActiveLine())
if (options.highlightSelectionMatches !== false)
extensions.push(highlightSelectionMatches())

return extensions.concat([keymap.of(keymaps.flat())]).filter(Boolean)
}
Expand Down Expand Up @@ -164,11 +177,14 @@ export const minimalSetup = (options: MinimalSetupOptions = {}) => {
}

const extensions: Extension[] = []
if (options.highlightSpecialChars !== false) extensions.push(highlightSpecialChars())
if (options.highlightSpecialChars !== false)
extensions.push(highlightSpecialChars())
if (options.history !== false) extensions.push(history())
if (options.drawSelection !== false) extensions.push(drawSelection())
if (options.syntaxHighlighting !== false)
extensions.push(syntaxHighlighting(defaultHighlightStyle, { fallback: true }))
extensions.push(
syntaxHighlighting(defaultHighlightStyle, { fallback: true })
)

return extensions.concat([keymap.of(keymaps.flat())]).filter(Boolean)
}
7 changes: 7 additions & 0 deletions packages/extensions/basic-setup/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}
36 changes: 36 additions & 0 deletions packages/extensions/cur-sql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@tidbcloud/tisqleditor-extensions-cur-sql",
"version": "0.0.1",
"description": "tisqleditor extensions",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/*.js",
"dist/*.ts",
"package.json",
"README.md"
],
"scripts": {
"build": "tsc && rollup -c"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.26.3",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^4.18.0",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
},
"peerDependencies": {
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.26.3"
},
"dependencies": {
"@tidbcloud/tisqleditor-extensions-sql-parser": "workspace:^"
}
}
10 changes: 10 additions & 0 deletions packages/extensions/cur-sql/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typescript from '@rollup/plugin-typescript'

export default {
input: './src/index.ts',
output: {
dir: './dist',
format: 'es'
},
plugins: [typescript()]
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { StateField, StateEffect, EditorState } from '@codemirror/state'
import { EditorView, ViewUpdate } from '@codemirror/view'

import { SqlStatement, getSqlStatements } from './sql-parser'
import {
SqlStatement,
getSqlStatements
} from '@tidbcloud/tisqleditor-extensions-sql-parser'

// state effect
const curStatementsEffect = StateEffect.define<SqlStatement[]>()

// state field
const curStatementsField = StateField.define<SqlStatement[]>({
create() {
return [{ from: 0, to: 0, lineFrom: 1, lineTo: 1, content: '', database: '', type: 'other' }]
return [
{
from: 0,
to: 0,
lineFrom: 1,
lineTo: 1,
content: '',
database: '',
type: 'other'
}
]
},
update(value, tr) {
for (let effect of tr.effects) {
Expand Down Expand Up @@ -76,7 +89,8 @@ export function getCurStatements(state: EditorState) {

export function getCurDatabase(state: EditorState) {
const curStatements = getCurStatements(state)
if (curStatements.length === 0) throw new Error('curStatements must not be empty')
if (curStatements.length === 0)
throw new Error('curStatements must not be empty')
return curStatements[0].database
}

Expand Down
7 changes: 7 additions & 0 deletions packages/extensions/cur-sql/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}
33 changes: 33 additions & 0 deletions packages/extensions/lang-sql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@tidbcloud/tisqleditor-extensions-lang-sql",
"version": "0.0.1",
"description": "tisqleditor extensions",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/*.js",
"dist/*.ts",
"package.json",
"README.md"
],
"scripts": {
"build": "tsc && rollup -c"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@codemirror/language": "^6.10.2",
"@codemirror/lang-sql": "^6.6.4",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^4.18.0",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
},
"peerDependencies": {
"@codemirror/language": "^6.10.2",
"@codemirror/lang-sql": "^6.6.4"
}
}
10 changes: 10 additions & 0 deletions packages/extensions/lang-sql/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typescript from '@rollup/plugin-typescript'

export default {
input: './src/index.ts',
output: {
dir: './dist',
format: 'es'
},
plugins: [typescript()]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MySQL, sql, SQLConfig } from '@codemirror/lang-sql'
import { LanguageSupport } from '@codemirror/language'

export const langSql = (config: SQLConfig) =>
export const langSql = (config: SQLConfig): LanguageSupport =>
sql({
dialect: MySQL,
upperCaseKeywords: true,
Expand Down
7 changes: 7 additions & 0 deletions packages/extensions/lang-sql/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}
10 changes: 0 additions & 10 deletions packages/extensions/rollup.config.js

This file was deleted.

35 changes: 35 additions & 0 deletions packages/extensions/sql-parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@tidbcloud/tisqleditor-extensions-sql-parser",
"version": "0.0.1",
"description": "tisqleditor extensions",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/*.js",
"dist/*.ts",
"package.json",
"README.md"
],
"scripts": {
"build": "tsc && rollup -c"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@codemirror/language": "^6.10.2",
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.26.3",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^4.18.0",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
},
"peerDependencies": {
"@codemirror/language": "^6.10.2",
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.26.3"
}
}
10 changes: 10 additions & 0 deletions packages/extensions/sql-parser/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typescript from '@rollup/plugin-typescript'

export default {
input: './src/index.ts',
output: {
dir: './dist',
format: 'es'
},
plugins: [typescript()]
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const statementsField = StateField.define<SqlStatement[]>({
// - use 'test';
// - use "test";
export const useStatementRegex = /^use\s+[`'"]?([a-zA-Z0-9_-]+)[`'"]?;$/i
const ddlStatementRegex = /^(create|drop|alter|truncate|rename|comment|grant|revoke)/i
const ddlStatementRegex =
/^(create|drop|alter|truncate|rename|comment|grant|revoke)/i

// event listener
const statementsParser = () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/extensions/sql-parser/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}
4 changes: 0 additions & 4 deletions packages/extensions/src/index.ts

This file was deleted.

Loading

0 comments on commit b7fad88

Please sign in to comment.