(
public plugin: CodeMirrorPromptPlugin,
public from: number,
public to: number
)
| 67 | protected container: HTMLDivElement; |
| 68 | |
| 69 | constructor( |
| 70 | public plugin: CodeMirrorPromptPlugin, |
| 71 | public from: number, |
| 72 | public to: number |
| 73 | ) { |
| 74 | super(); |
| 75 | |
| 76 | // Generate unique session id for this prompt |
| 77 | const sessionId = generateId(); |
| 78 | |
| 79 | plugin.lock(); |
| 80 | this.container = document.createElement("div"); |
| 81 | |
| 82 | plugin.isActive = true; |
| 83 | plugin.activeWidget = this; |
| 84 | |
| 85 | const view = plugin.getEditorView(); |
| 86 | if (!view) return; |
| 87 | |
| 88 | // First we need to lock the editor to read-only. |
| 89 | // This will simplify the logic of the plugin of not having to |
| 90 | // worry about keeping up with the editor state while the prompt |
| 91 | const getSelectionLines = () => { |
| 92 | const startLineNumber = view.state.doc.lineAt(from).number; |
| 93 | const endLineNumber = view.state.doc.lineAt(to).number; |
| 94 | |
| 95 | return Array.from( |
| 96 | { length: endLineNumber - startLineNumber + 1 }, |
| 97 | (_, i) => startLineNumber + i |
| 98 | ); |
| 99 | }; |
| 100 | |
| 101 | const selectedLines = getSelectionLines(); |
| 102 | |
| 103 | // Calculate cursor and selection positions |
| 104 | const startPosition = from; |
| 105 | const startLineNumber = view.state.doc.lineAt(startPosition).number; |
| 106 | const endPosition = selectedLines |
| 107 | ? view.state.doc.line(selectedLines[selectedLines.length - 1]).to |
| 108 | : view.state.doc.lineAt(startPosition).to; |
| 109 | |
| 110 | // Reverse the suggestion that we have made |
| 111 | const selectedOriginalText = view.state.sliceDoc( |
| 112 | startPosition, |
| 113 | endPosition |
| 114 | ); |
| 115 | |
| 116 | const originalText = view.state.doc.toString(); |
| 117 | let suggestedText = selectedOriginalText; |
| 118 | |
| 119 | const reverseSuggestion = () => { |
| 120 | if (suggestedText !== selectedOriginalText) { |
| 121 | this.plugin.hideSuggestionDiff(); |
| 122 | |
| 123 | view.dispatch({ |
| 124 | changes: { |
| 125 | from: startPosition, |
| 126 | to: startPosition + suggestedText.length, |
nothing calls this directly
no test coverage detected