Skip to content

Commit 1fa35b5

Browse files
authored
Merge pull request #1313 from HerrEmil/warning-column-position
Display Cypher warnings on correct positions
2 parents 065c1fd + d14fe94 commit 1fa35b5

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

src/browser/custom.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,20 @@ declare module 'cypher-editor-support' {
118118
setSchema(schema: EditorSupportSchema): void
119119
update(input: string): void
120120
}
121+
interface CypherPosition {
122+
column: number
123+
line: number
124+
}
125+
export interface QueryOrCommand {
126+
getText: () => string
127+
start: CypherPosition
128+
stop: CypherPosition
129+
}
121130
export function parse(
122131
input: string
123132
): {
124133
referencesListener: {
125-
queriesAndCommands: { getText: () => string; start: { line: number } }[]
134+
queriesAndCommands: QueryOrCommand[]
126135
}
127136
}
128137
export function extractStatements(

src/browser/modules/Editor/Monaco.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020

2121
import { QuickInputList } from 'monaco-editor/esm/vs/base/parts/quickinput/browser/quickInputList'
22-
import { parse } from 'cypher-editor-support'
22+
import { parse, QueryOrCommand } from 'cypher-editor-support'
2323
import { debounce } from 'lodash-es'
2424
import {
2525
editor,
@@ -40,6 +40,7 @@ import { Bus } from 'suber'
4040

4141
import { NEO4J_BROWSER_USER_ACTION_QUERY } from 'services/bolt/txMetadata'
4242
import { CYPHER_REQUEST } from 'shared/modules/cypher/cypherDuck'
43+
import { NotificationPosition, QueryResult } from 'neo4j-driver'
4344

4445
const shouldCheckForHints = (code: string) =>
4546
code.trim().length > 0 &&
@@ -89,6 +90,8 @@ interface MonacoProps {
8990
toggleFullscreen: () => void
9091
}
9192

93+
const EXPLAIN_QUERY_PREFIX = 'EXPLAIN '
94+
const EXPLAIN_QUERY_PREFIX_LENGTH = EXPLAIN_QUERY_PREFIX.length
9295
const Monaco = forwardRef<MonacoHandles, MonacoProps>(
9396
(
9497
{
@@ -405,9 +408,7 @@ const Monaco = forwardRef<MonacoHandles, MonacoProps>(
405408
debouncedUpdateCode()
406409
}
407410

408-
const addWarnings = (
409-
statements: { start: { line: number }; getText: () => string }[]
410-
) => {
411+
const addWarnings = (statements: QueryOrCommand[]) => {
411412
if (!statements.length) return
412413

413414
const model = editorRef.current?.getModel() as editor.ITextModel
@@ -446,33 +447,33 @@ const Monaco = forwardRef<MonacoHandles, MonacoProps>(
446447
bus.self(
447448
CYPHER_REQUEST,
448449
{
449-
query: 'EXPLAIN ' + text,
450+
query: EXPLAIN_QUERY_PREFIX + text,
450451
queryType: NEO4J_BROWSER_USER_ACTION_QUERY
451452
},
452-
response => {
453+
(response: { result: QueryResult; success?: boolean }) => {
453454
if (
454455
response.success === true &&
455456
response.result.summary.notifications.length > 0
456457
) {
457458
editor.setModelMarkers(model, monacoId, [
458459
...editor.getModelMarkers({ owner: monacoId }),
459460
...response.result.summary.notifications.map(
460-
({
461-
description,
462-
position: { line },
463-
title
464-
}: {
465-
description: string
466-
position: { line: number }
467-
title: string
468-
}) => ({
469-
startLineNumber: statementLineNumber + line,
470-
startColumn: 1,
471-
endLineNumber: statementLineNumber + line,
472-
endColumn: 1000,
473-
message: title + '\n\n' + description,
474-
severity: MarkerSeverity.Warning
475-
})
461+
({ description, position, title }) => {
462+
const line = 'line' in position ? position.line : 0
463+
const column = 'column' in position ? position.column : 0
464+
return {
465+
startLineNumber: statementLineNumber + line,
466+
startColumn:
467+
statement.start.column +
468+
(line === 1
469+
? column - EXPLAIN_QUERY_PREFIX_LENGTH
470+
: column),
471+
endLineNumber: statement.stop.line,
472+
endColumn: statement.stop.column + 2,
473+
message: title + '\n\n' + description,
474+
severity: MarkerSeverity.Warning
475+
}
476+
}
476477
)
477478
])
478479
}

0 commit comments

Comments
 (0)