MCPcopy
hub / github.com/arduino/Arduino / setText

Method setText

app/src/processing/app/EditorTab.java:408–448  ·  view source on GitHub ↗

Replace the entire contents of this tab.

(String what)

Source from the content-addressed store, hash-verified

406 * Replace the entire contents of this tab.
407 */
408 public void setText(String what) {
409 // Remove all highlights, since these will all end up at the start of the
410 // text otherwise. Preserving them is tricky, so better just remove them.
411 textarea.removeAllLineHighlights();
412 // Set the caret update policy to NEVER_UPDATE while completely replacing
413 // the current text. Normally, the caret tracks inserts and deletions, but
414 // replacing the entire text will always make the caret end up at the end,
415 // which isn't really useful. With NEVER_UPDATE, the caret will just keep
416 // its absolute position (number of characters from the start), which isn't
417 // always perfect, but the best we can do without making a diff of the old
418 // and new text and some guesswork.
419 // Note that we cannot use textarea.setText() here, since that first removes
420 // text and then inserts the new text. Even with NEVER_UPDATE, the caret
421 // always makes sure to stay valid, so first removing all text makes it
422 // reset to 0. Also note that simply saving and restoring the caret position
423 // will work, but then the scroll position might change in response to the
424 // caret position.
425 DefaultCaret caret = (DefaultCaret) textarea.getCaret();
426 int policy = caret.getUpdatePolicy();
427 caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
428 try {
429 Document doc = textarea.getDocument();
430 int oldLength = doc.getLength();
431 // The undo manager already seems to group the insert and remove together
432 // automatically, but better be explicit about it.
433 textarea.beginAtomicEdit();
434 try {
435 doc.insertString(oldLength, what, null);
436 doc.remove(0, oldLength);
437 } catch (BadLocationException e) {
438 System.err.println("Unexpected failure replacing text");
439 } finally {
440 textarea.endAtomicEdit();
441 }
442 } finally {
443 caret.setUpdatePolicy(policy);
444 }
445 // A trick to force textarea to recalculate the bracket matching rectangle.
446 // In the worst case scenario, this should be ineffective.
447 textarea.setLineWrap(textarea.getLineWrap());
448 }
449
450 /**
451 * Is the text modified since the last save / load?

Callers 13

reloadMethod · 0.95
SerialMonitorMethod · 0.45
keyPressedMethod · 0.45
actionPerformedMethod · 0.45
closeMethod · 0.45
menuSelectedMethod · 0.45
importLibraryMethod · 0.45
onCreateWindowMethod · 0.45
editMethod · 0.45
keyTypedMethod · 0.45
runMethod · 0.45

Calls 3

insertStringMethod · 0.80
removeMethod · 0.45

Tested by

no test coverage detected