@@ -34,12 +34,18 @@ import {event} from 'src/cloud/utils/reporting'
3434
3535// hardcoded in LSP
3636const 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
3944class 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