Skip to content

Commit 29fc117

Browse files
authored
feat(6063): suppport import statements with composition (#6088)
* chore(6063): add testcase for the composition supporting import statements outside block * feat(6063): allow onChange detection based on snapshots of editor tech prior to change. That way we can compare the change postional info, with the snapshot state (where the composition block was) at the time that the change was applied. This also allows us to have lines added before the composition block, such as import statements, without diverging. * chore: debug fluxQueryBuilder test suite
1 parent 9a37579 commit 29fc117

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

cypress/e2e/shared/fluxQueryBuilder.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,27 @@ describe('Script Builder', () => {
504504
'disabled'
505505
)
506506

507+
cy.log(
508+
'does not diverge, when adding import statement outside of block'
509+
)
510+
cy.getByTestID('flux-toolbar-search--input')
511+
.should('exist')
512+
.type('fieldsAsCols')
513+
cy.get('.flux-toolbar--list-item').first().click()
514+
cy.getByTestID('flux-editor').contains('import')
515+
cy.getByTestID('flux-sync--toggle').should(
516+
'not.have.class',
517+
'disabled'
518+
)
519+
520+
/// FIXME: Does not work yet, since the LSP response for applyEdit starts at line 0
521+
// cy.log(
522+
// 'does not diverge, when further modifying block (with imports at top)'
523+
// )
524+
507525
cy.log('does diverge, within block')
508526
cy.getByTestID('flux-editor').monacoType(
509-
'{enter}{upArrow}{upArrow}{upArrow} // make diverge'
527+
'{enter}{upArrow}{upArrow}{upArrow}{upArrow} // make diverge'
510528
)
511529
cy.log('toggle is now disabled')
512530
cy.getByTestID('flux-sync--toggle').should('have.class', 'disabled')

src/languageSupport/languages/flux/lsp/connection.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ import {event} from 'src/cloud/utils/reporting'
3434

3535
// hardcoded in LSP
3636
const COMPOSITION_YIELD = '_editor_composition'
37-
const COMPOSITION_INIT_LINE = 1
37+
38+
const findLastIndex = (arr, fn) =>
39+
(arr
40+
.map((val, i) => [i, val])
41+
.filter(([i, val]) => fn(val, i, arr))
42+
.pop() || [-1])[0]
3843

3944
class LspConnectionManager {
4045
private _worker: Worker
4146
private _editor: EditorType
4247
private _model: MonacoTypes.editor.IModel
48+
private _snapshot: MonacoTypes.editor.ITextSnapshot
4349
private _preludeModel: MonacoTypes.editor.IModel
4450
private _variables: Variable[] = []
4551
private _compositionStyle: string[] = []
@@ -121,14 +127,19 @@ class LspConnectionManager {
121127
this._worker.postMessage(msg)
122128
}
123129

124-
_getCompositionBlockLines() {
125-
const query = this._model.getValue()
126-
if (!query.includes(COMPOSITION_YIELD)) {
130+
_getCompositionBlockLines(query) {
131+
if (!query || !query.includes(COMPOSITION_YIELD)) {
127132
return null
128133
}
129-
const startLine = COMPOSITION_INIT_LINE
134+
const lines = query.split('\n')
135+
// monacoEditor line indexing starts at 1..n
130136
const endLine =
131-
query.split('\n').findIndex(line => line.includes(COMPOSITION_YIELD)) + 1
137+
lines.findIndex(line => line.includes(COMPOSITION_YIELD)) + 1
138+
const startLine =
139+
findLastIndex(lines.slice(0, endLine - 1), line =>
140+
line.includes('from(')
141+
) + 1
142+
132143
return {startLine, endLine}
133144
}
134145

@@ -154,7 +165,11 @@ class LspConnectionManager {
154165
}
155166

156167
_editorChangeIsWithinComposition(change) {
157-
const compositionBlock = this._getCompositionBlockLines()
168+
const compositionBlock = this._getCompositionBlockLines(
169+
this._snapshot.read()
170+
)
171+
this._snapshot = this._model.createSnapshot()
172+
158173
if (!compositionBlock) {
159174
return false
160175
}
@@ -196,7 +211,9 @@ class LspConnectionManager {
196211
})
197212

198213
comments(this._editor, selection => {
199-
const compositionBlock = this._getCompositionBlockLines()
214+
const compositionBlock = this._getCompositionBlockLines(
215+
this._model.getValue()
216+
)
200217
if (!compositionBlock) {
201218
return
202219
}
@@ -241,19 +258,21 @@ class LspConnectionManager {
241258
}
242259

243260
_setEditorBlockStyle(schema: CompositionSelection = this._session) {
244-
const compositionBlock = this._getCompositionBlockLines()
261+
const compositionBlock = this._getCompositionBlockLines(
262+
this._model.getValue()
263+
)
245264

246265
const removeAllStyles = !compositionBlock || schema.composition.diverged
247266

248-
const compositionSyncStyle = this._compositionSyncStyle(
249-
compositionBlock?.startLine,
250-
compositionBlock?.endLine,
251-
schema.composition.synced
252-
)
253-
254267
this._compositionStyle = this._editor.deltaDecorations(
255268
this._compositionStyle,
256-
removeAllStyles ? [] : compositionSyncStyle
269+
removeAllStyles
270+
? []
271+
: this._compositionSyncStyle(
272+
compositionBlock?.startLine,
273+
compositionBlock?.endLine,
274+
schema.composition.synced
275+
)
257276
)
258277
}
259278

@@ -404,6 +423,8 @@ class LspConnectionManager {
404423
}
405424

406425
_initCompositionHandlers() {
426+
this._snapshot = this._model.createSnapshot()
427+
407428
// handlers to trigger end composition
408429
this._setEditorIrreversibleExit()
409430

0 commit comments

Comments
 (0)