Skip to content

Commit b021b25

Browse files
committed
Ability to run code selection. Selecting whitespace runs the whole script.
Signed-off-by: ubi de feo <[email protected]>
1 parent c5b49c3 commit b021b25

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

ui/arduino/store.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,23 @@ async function store(state, emitter) {
193193
emitter.on('run', async () => {
194194
log('run')
195195
const openFile = state.openFiles.find(f => f.id == state.editingFile)
196-
const code = openFile.editor.editor.state.doc.toString()
196+
let code = openFile.editor.editor.state.doc.toString()
197+
198+
// If there is a selection, run only the selected code
199+
const startIndex = openFile.editor.editor.state.selection.ranges[0].from
200+
const endIndex = openFile.editor.editor.state.selection.ranges[0].to
201+
if (endIndex - startIndex > 0) {
202+
selectedCode = openFile.editor.editor.state.doc.toString().substring(startIndex, endIndex)
203+
// Checking to see if the user accidentally double-clicked some whitespace
204+
// While a random selection would yield an error when executed,
205+
// selecting only whitespace would not and the user would have no feedback.
206+
// This check only replaces the full content of the currently selected tab
207+
// with a text selection if the selection is not empty and contains only whitespace.
208+
if (selectedCode.trim().length > 0) {
209+
code = selectedCode
210+
}
211+
}
212+
197213
emitter.emit('open-panel')
198214
emitter.emit('render')
199215
try {

0 commit comments

Comments
 (0)