Skip to content

Commit

Permalink
Merge branch 'main' into test-published-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
baurine authored Jul 4, 2024
2 parents 32ffe47 + db393ee commit 82cdf38
Show file tree
Hide file tree
Showing 48 changed files with 673 additions and 491 deletions.
61 changes: 18 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,57 +40,28 @@ https://github.com/tidbcloud/tisqleditor/assets/1284531/732b600f-5b4e-45d3-a3d2-

See [editor.tsx](./packages/playground/src/components/biz/editor-panel/editor.tsx) or [editor-example.tsx](./packages/playground/src/examples/editor-example.tsx) to get more details.

```shell
pnpm add @tidbcloud/tisqleditor-react @tidbcloud/codemirror-extension-themes @tidbcloud/codemirror-extension-cur-sql-gutter
```

```tsx
import { SQLEditor } from '@tidbcloud/tisqleditor-react'
import { bbedit, oneDark } from '@tidbcloud/codemirror-extension-themes'
import { saveHelper } from '@tidbcloud/codemirror-extension-save-helper'
import { oneDark } from '@tidbcloud/codemirror-extension-themes'
import { curSqlGutter } from '@tidbcloud/codemirror-extension-cur-sql-gutter'
import {
useDbLinter,
fullWidthCharLinter
} from '@tidbcloud/codemirror-extension-linters'
import { sqlAutoCompletion } from '@tidbcloud/codemirror-extension-sql-autocomplete'
import {
aiWidget,
isUnifiedMergeViewActive
} from '@tidbcloud/codemirror-extension-ai-widget'

export function Editor() {
const extraExts = [
saveHelper({
save: (view: EditorView) => {
saveFile(activeFile.id, view.state.doc.toString())
}
}),
sqlAutoCompletion(),
curSqlGutter({
whenHide: (view) => {
return isUnifiedMergeViewActive(view.state)
}
}),
useDbLinter(),
fullWidthCharLinter(),
aiWidget({
chat(view, chatId, req) {
return chatCtx.chat(chatId, req)
},
cancelChat: chatCtx.cancelChat,
onEvent(_view, type, payload) {
chatCtx.onEvent(type, payload)
},
getDbList: getDbListRef.current!
})
]

return (
<SQLEditor
className="h-full"
editorId={activeFile.id}
doc={activeFile.content}
sqlConfig={sqlConfig}
editorId="MySQLEditor"
doc={'USE game;\n'}
theme={oneDark}
// theme={bbedit}
extraExts={extraExts}
basicSetupOptions={{
autocompletion: true
}}
extraExts={[
curSqlGutter()
// here you can add some other extensions as you need
]}
/>
)
}
Expand Down Expand Up @@ -127,6 +98,10 @@ Before you create a pull request, please check whether your commits comply with
- refactor: any code related change that is not a fix nor a feature
- chore: all changes to the repository that do not fit into any of the above categories

### Test

To run the test, execute the command `pnpm test`

### Release

- Checkout from the latest main branch.
Expand Down
10 changes: 10 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# @tidbcloud/tisqleditor

## 0.0.4

### Patch Changes

- 122f217: fix: update readme & some config params
- Updated dependencies [122f217]
- @tidbcloud/codemirror-extension-basic-setup@0.0.4
- @tidbcloud/codemirror-extension-cur-sql@0.0.4
- @tidbcloud/codemirror-extension-sql-parser@0.0.4

## 0.0.3

### Patch Changes
Expand Down
10 changes: 9 additions & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This package provides the `SQLEditorInstance` and `EditorCache` implementation.

`SQLEditorInstance` creates EditorView instance with pre-configured extensions to make it available to edit SQL code.
`SQLEditorInstance` creates EditorView instance with pre-configured extensions to make it available to edit SQL code, likes `@codemirror/lang-sql`, `@tidbcloud/codemirror-extension-sql-parser`, `@tidbcloud/codemirror-extension-cur-sql`.

`EditorCache` stores the `SQLEditorInstance` in a map.

Expand Down Expand Up @@ -33,6 +33,14 @@ const editorInst = createSQLEditorInstance({
cache.addEditor(editorId, editorInst)
```

The package installs the cur-sql and sql-parser extensions default, can use the following methods:

```ts
const curSql = editorInst.getCurStatements()
const allSqls = editorInst.getAllStatements()
const nearbySql = editorInst.getNearbyStatement()
```

## API

### createSQLEditorInstance
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tidbcloud/tisqleditor",
"version": "0.0.3",
"version": "0.0.4",
"description": "editor instance and editor cache",
"type": "module",
"main": "dist/index.js",
Expand Down
34 changes: 22 additions & 12 deletions packages/core/src/test/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import { createSQLEditorInstance, EditorCache } from '..'

test('editor instance cache works fine', () => {
describe('editor instance cache works fine', () => {
const cache = new EditorCache()
const editorInst = createSQLEditorInstance({
editorId: '111',
doc: 'select * from test;'
})

expect(cache.getEditor('111')).toBe(undefined)
test('no cache added', () => {
expect(cache.getEditor('111')).toBe(undefined)
})

cache.addEditor('111', editorInst)
expect(cache.getEditor('111')).toBe(editorInst)
test('add one instance 111 to cache', () => {
cache.addEditor('111', editorInst)
expect(cache.getEditor('111')).toBe(editorInst)
})

cache.deleteEditor('222')
expect(cache.getEditor('111')).toBe(editorInst)
test('remove instance 222 from cache', () => {
cache.deleteEditor('222')
expect(cache.getEditor('111')).toBe(editorInst)
})

cache.deleteEditor('111')
expect(cache.getEditor('111')).toBe(undefined)
test('remove instance 111 from cache', () => {
cache.deleteEditor('111')
expect(cache.getEditor('111')).toBe(undefined)
})

cache.addEditor('111', editorInst)
expect(cache.getEditor('111')).toBe(editorInst)
test('clear cache', () => {
cache.addEditor('111', editorInst)
expect(cache.getEditor('111')).toBe(editorInst)

cache.clearEditors()
expect(cache.getEditor('111')).toBe(undefined)
cache.clearEditors()
expect(cache.getEditor('111')).toBe(undefined)
})
})
116 changes: 77 additions & 39 deletions packages/core/src/test/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,94 @@ ORDER BY sector, companies DESC;`

const DOC = `\n${LINE_1}\n\n${LINE_2}\n\n`

test('editor instance works fine', () => {
const editorInst = createSQLEditorInstance({
editorId: '111',
doc: DOC
describe('editor instance works fine', () => {
let editorInst

beforeAll(() => {
editorInst = createSQLEditorInstance({
editorId: '111',
doc: DOC
})
})

expect(editorInst.editorId).toBe('111')
test('editorId should be correct', () =>
expect(editorInst.editorId).toBe('111'))

editorInst.editorView.dispatch({ selection: { anchor: 0, head: 0 } })
describe('dispatch a empty selection action', () => {
let curSql, nearbySql, allSql

const allSql = editorInst.getAllStatements()
expect(allSql.length).toBe(2)
expect(allSql[0].content).toBe(LINE_1)
expect(allSql[1].content).toBe(LINE_2)
beforeAll(() => {
editorInst.editorView.dispatch({ selection: { anchor: 0, head: 0 } })
curSql = editorInst.getCurStatements()
nearbySql = editorInst.getNearbyStatement()
allSql = editorInst.getAllStatements()
})

let curSql = editorInst.getCurStatements()
expect(curSql[0].content).toBe('')
test('the content and length of statement is correct', () => {
expect(allSql.length).toBe(2)
expect(allSql[0].content).toBe(LINE_1)
expect(allSql[1].content).toBe(LINE_2)
})

let nearbySql = editorInst.getNearbyStatement()
expect(nearbySql?.content).toBe(LINE_1)
test('current sql is empty', () => {
expect(curSql[0].content).toBe('')
})

editorInst.editorView.dispatch({
selection: { anchor: 1, head: 2 }
test('nearby sql is line1', () => {
expect(nearbySql?.content).toBe(LINE_1)
})
})

curSql = editorInst.getCurStatements()
expect(curSql[0].content).toBe(LINE_1)

nearbySql = editorInst.getNearbyStatement()
expect(nearbySql?.content).toBe(LINE_1)

editorInst.editorView.dispatch({
selection: { anchor: DOC.length, head: DOC.length }
describe('dispatch a selection action', () => {
let curSql, nearbySql

beforeAll(() => {
editorInst.editorView.dispatch({
selection: { anchor: 1, head: 2 }
})
curSql = editorInst.getCurStatements()
nearbySql = editorInst.getNearbyStatement()
})

test('current sql is line1, nearby sql is line1', () => {
expect(curSql[0].content).toBe(LINE_1)
expect(nearbySql?.content).toBe(LINE_1)
})
})

curSql = editorInst.getCurStatements()
expect(curSql[0].content).toBe('')

nearbySql = editorInst.getNearbyStatement()
expect(nearbySql?.content).toBe(LINE_2)

editorInst.editorView.dispatch({
selection: { anchor: 2, head: 2 + LINE_1.length + 5 }
describe('dispatch a selection action', () => {
let curSql, nearbySql

beforeAll(() => {
editorInst.editorView.dispatch({
selection: { anchor: DOC.length, head: DOC.length }
})
curSql = editorInst.getCurStatements()
nearbySql = editorInst.getNearbyStatement()
})

test('current sql is empty, nearby sql is line2', () => {
expect(curSql[0].content).toBe('')
expect(nearbySql?.content).toBe(LINE_2)
})
})

curSql = editorInst.getCurStatements()
expect(curSql.length).toBe(2)
expect(curSql[0].content).toBe(LINE_1)
expect(curSql[1].content).toBe(LINE_2)

nearbySql = editorInst.getNearbyStatement()
expect(nearbySql?.content).toBe(LINE_1)
describe('dispatch a selection action', () => {
let curSql, nearbySql

beforeAll(() => {
editorInst.editorView.dispatch({
selection: { anchor: 2, head: 2 + LINE_1.length + 5 }
})
curSql = editorInst.getCurStatements()
nearbySql = editorInst.getNearbyStatement()
})

test('current sql length is 2, nearby sql is line1', () => {
expect(curSql.length).toBe(2)
expect(curSql[0].content).toBe(LINE_1)
expect(curSql[1].content).toBe(LINE_2)
expect(nearbySql?.content).toBe(LINE_1)
})
})
})
8 changes: 8 additions & 0 deletions packages/extensions/ai-widget/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @tidbcloud/codemirror-extension-ai-widget

## 0.0.4

### Patch Changes

- 122f217: fix: update readme & some config params
- Updated dependencies [122f217]
- @tidbcloud/codemirror-extension-cur-sql@0.0.4

## 0.0.3

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/extensions/ai-widget/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @tidbcloud/codemirror-extension-ai-widget

A codemirror extension provides a widget to chat with AI to help you write or refine SQL. (work with other languages wip)
A codemirror extension provides a widget to chat with AI to help you write or refine SQL. (will support to work with other languages later)

https://github.com/tidbcloud/tisqleditor/assets/1284531/46684333-7efa-4925-bf58-9ab3fb45f692

Expand All @@ -22,10 +22,10 @@ https://github.com/tidbcloud/tisqleditor/assets/1284531/46684333-7efa-4925-bf58-
npm install @tidbcloud/codemirror-extension-ai-widget
```

You need to install its peer dependencies as well:
You need to install its dependencies as well:

```shell
npm install @codemirror/view @codemirror/state @codemirror/merge @codemirror/lang-sql
npm install @codemirror/view @codemirror/state @codemirror/merge @codemirror/lang-sql @tidbcloud/codemirror-extension-sql-parser @tidbcloud/codemirror-extension-cur-sql
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion packages/extensions/ai-widget/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tidbcloud/codemirror-extension-ai-widget",
"version": "0.0.3",
"version": "0.0.4",
"description": "ai widget extension for codemirror",
"type": "module",
"main": "dist/index.js",
Expand Down
6 changes: 6 additions & 0 deletions packages/extensions/basic-setup/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tidbcloud/codemirror-extension-basic-setup

## 0.0.4

### Patch Changes

- 122f217: fix: update readme & some config params

## 0.0.3

### Patch Changes
Expand Down
Loading

0 comments on commit 82cdf38

Please sign in to comment.