-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CB-5185 SQL Editor queries highlighting is broken #2842
Changes from all commits
d801125
50b5420
a99cdfc
1def9f8
9384769
9a032a8
f7b5ed4
eaf5646
fc4eca2
9a03973
f002c4a
1a193ad
5497ef9
417c0cc
c24f436
79263d3
3e25251
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import { action } from 'mobx'; | |
import { useCallback } from 'react'; | ||
|
||
import { useExecutor, useObservableRef } from '@cloudbeaver/core-blocks'; | ||
import { throttle } from '@cloudbeaver/core-utils'; | ||
import { debounce } from '@cloudbeaver/core-utils'; | ||
import type { ISQLEditorData } from '@cloudbeaver/plugin-sql-editor'; | ||
|
||
import type { IEditor } from '../SQLCodeEditor/useSQLCodeEditor'; | ||
|
@@ -21,16 +21,20 @@ interface State { | |
} | ||
|
||
export function useSQLCodeEditorPanel(data: ISQLEditorData, editor: IEditor) { | ||
const script = data.dataSource?.script; | ||
|
||
const state: State = useObservableRef( | ||
() => ({ | ||
highlightActiveQuery() { | ||
this.editor.clearActiveQueryHighlight(); | ||
queueMicrotask(() => { | ||
this.editor.clearActiveQueryHighlight(); | ||
|
||
const segment = this.data.activeSegment; | ||
const segment = this.data.activeSegment; | ||
|
||
if (segment) { | ||
this.editor.highlightActiveQuery(segment.begin, segment.end); | ||
} | ||
if (segment && data.isCursorInActiveQuerySegment) { | ||
this.editor.highlightActiveQuery(segment.begin, segment.end); | ||
} | ||
}); | ||
}, | ||
onQueryChange(query: string) { | ||
this.data.setScript(query); | ||
|
@@ -44,13 +48,29 @@ export function useSQLCodeEditorPanel(data: ISQLEditorData, editor: IEditor) { | |
); | ||
|
||
const updateHighlight = useCallback( | ||
throttle(() => state.highlightActiveQuery(), 1000), | ||
debounce(() => state.highlightActiveQuery(), 300), | ||
[state], | ||
); | ||
|
||
function onUpdate() { | ||
const newScript = data.dataSource?.script; | ||
const isScriptChanged = script !== newScript; | ||
|
||
editor.clearActiveQueryHighlight(); | ||
|
||
if (!isScriptChanged) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is for the case when we just move the cursor with mouse or keyboard arrows |
||
updateHighlight(); | ||
} | ||
} | ||
|
||
useExecutor({ | ||
executor: data.onUpdate, | ||
handlers: [updateHighlight], | ||
handlers: [onUpdate], | ||
}); | ||
|
||
useExecutor({ | ||
executor: data.dataSource?.onUpdate, | ||
handlers: [onUpdate], | ||
}); | ||
|
||
useExecutor({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ interface ISQLEditorDataPrivate extends ISQLEditorData { | |
} | ||
|
||
const MAX_HINTS_LIMIT = 200; | ||
const END_OF_THE_ACTIVE_QUERY_OFFSET = 2; | ||
|
||
export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { | ||
const connectionExecutionContextService = useService(ConnectionExecutionContextService); | ||
|
@@ -88,6 +89,13 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { | |
activeSegmentMode: false, | ||
}, | ||
|
||
get isCursorInActiveQuerySegment(): boolean { | ||
const segment = this.activeSegment; | ||
const cursor = this.cursorSegment; | ||
|
||
return !!segment && !!cursor && cursor.begin >= segment.begin && cursor.end - END_OF_THE_ACTIVE_QUERY_OFFSET <= segment.end; | ||
}, | ||
|
||
get activeSegment(): ISQLScriptSegment | undefined { | ||
return this.activeSegmentMode.activeSegment; | ||
}, | ||
|
@@ -441,16 +449,16 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { | |
return this.getSubQuery(); | ||
} | ||
|
||
if (this.activeSegmentMode.activeSegmentMode) { | ||
return this.activeSegment; | ||
} | ||
|
||
const result = await this.sqlEditorService.parseSQLQuery(projectId, connectionId, this.value, this.cursor.begin); | ||
|
||
if (result.end === 0 && result.start === 0) { | ||
return; | ||
} | ||
|
||
if (this.activeSegment) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved it below cause I don't want to execute the query if selected line of SQL is empty (no query) also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please revert, this will affect other places, please in general don't touch code that work fine (our code related to query execution is working fine so there no need to change anything) when you have problem with different things (highlighting) |
||
return this.activeSegment; | ||
} | ||
|
||
const segment = this.parser.getSegment(result.start, result.end); | ||
return segment; | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is more performance and UX-friendly. Now with 120k rows in SQL editor I have almost no lags on my M1 PRO