MCPcopy Create free account
hub / github.com/BaseXdb/basex / sort

Method sort

basex-core/src/main/java/org/basex/gui/text/TextEditor.java:660–698  ·  view source on GitHub ↗

Sorts the selected text. @return true if text has changed

()

Source from the content-addressed store, hash-verified

658 * @return {@code true} if text has changed
659 */
660 boolean sort() {
661 if(!isSelected()) selectAll();
662 if(!extend()) return false;
663
664 // count lines
665 int l = 1;
666 final int s = start, e = end, ts = size();
667 final byte[] tmp = Arrays.copyOf(text, ts);
668 for(int i = s; i < e; i++) {
669 if(tmp[i] == '\n') l++;
670 }
671
672 // collect lines to be sorted
673 final TokenList tl = new TokenList(l);
674 final ByteList bl = new ByteList();
675 for(int i = s; i < e; i++) {
676 final byte ch = tmp[i];
677 if(ch == '\n') {
678 tl.add(bl.next());
679 } else {
680 bl.add(ch);
681 }
682 }
683 if(!bl.isEmpty()) tl.add(bl.finish());
684 sort(tl);
685
686 // copy lines back to text
687 int i = s;
688 for(final byte[] line : tl) {
689 final int ll = line.length;
690 Array.copyFromStart(line, ll, tmp, i);
691 i += ll;
692 if(i < e) tmp[i++] = '\n';
693 }
694 if(i < e) Array.copy(tmp, e, ts - e, tmp, i);
695 final boolean changed = text(i == e ? tmp : Arrays.copyOf(tmp, ts - e + i));
696 select(s, i);
697 return changed;
698 }
699
700 /**
701 * Sorts the specified data.

Callers

nothing calls this directly

Calls 15

isSelectedMethod · 0.95
selectAllMethod · 0.95
extendMethod · 0.95
sizeMethod · 0.95
addMethod · 0.95
nextMethod · 0.95
addMethod · 0.95
finishMethod · 0.95
copyFromStartMethod · 0.95
copyMethod · 0.95
textMethod · 0.95
selectMethod · 0.95

Tested by

no test coverage detected