(connection: Connection, documents: TextDocuments<AlphaTexTextDocument>)
| 44 | ]; |
| 45 | |
| 46 | export function setupCompletion(connection: Connection, documents: TextDocuments<AlphaTexTextDocument>) { |
| 47 | connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] => { |
| 48 | const document = documents.get(params.textDocument.uri); |
| 49 | if (!document?.ast) { |
| 50 | return topLevelCompletions; |
| 51 | } |
| 52 | |
| 53 | const offset = document.offsetAt(params.position); |
| 54 | |
| 55 | const bar = binaryNodeSearch(document.ast.bars, offset, Number.MAX_SAFE_INTEGER); |
| 56 | const barIndex = bar ? document.ast.bars.indexOf(bar) : 0; |
| 57 | |
| 58 | // no bar |
| 59 | if (!bar) { |
| 60 | return sortCompletions( |
| 61 | createMetaDataCompletions(barIndex, undefined, offset, Number.MAX_SAFE_INTEGER), |
| 62 | connection.console |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | const endOfBar = |
| 67 | bar.pipe?.start?.offset ?? |
| 68 | (barIndex === document.ast.bars.length - 1 |
| 69 | ? Number.MAX_SAFE_INTEGER |
| 70 | : document.ast.bars[barIndex + 1].start!.offset); |
| 71 | |
| 72 | const endOfBarMetaData = bar.beats[0]?.start?.offset ?? endOfBar; |
| 73 | const metaData = bar ? binaryNodeSearch(bar.metaData, offset, endOfBarMetaData) : undefined; |
| 74 | if (metaData) { |
| 75 | const metaDataIndex = bar.metaData.indexOf(metaData); |
| 76 | const endOfMetaData = |
| 77 | metaDataIndex === bar.metaData.length - 1 |
| 78 | ? endOfBarMetaData |
| 79 | : bar.metaData[metaDataIndex + 1].start!.offset; |
| 80 | return sortCompletions( |
| 81 | createMetaDataCompletions(barIndex, metaData, offset, endOfMetaData), |
| 82 | connection.console |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | const beat = binaryNodeSearch(bar.beats, offset, endOfBar); |
| 87 | if (beat) { |
| 88 | const beatIndex = bar.beats.indexOf(beat); |
| 89 | const endOfBeat = beatIndex === bar.beats.length - 1 ? endOfBar : bar.beats[beatIndex + 1].start!.offset; |
| 90 | |
| 91 | return sortCompletions(createBeatCompletions(beat, offset, endOfBeat), connection.console); |
| 92 | } |
| 93 | |
| 94 | return sortCompletions( |
| 95 | createMetaDataCompletions(barIndex, undefined, offset, Number.MAX_SAFE_INTEGER), |
| 96 | connection.console |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | connection.onCompletionResolve((item: CompletionItem): CompletionItem => { |
| 101 | return item; |
| 102 | }); |
| 103 | } |
no test coverage detected