Skip to content

Commit 055cfc8

Browse files
matthijskooijmanfacchinm
authored andcommitted
Simplify sorting in SketchData
Instead of manually sorting the primary file at the start, and fiddling to keep it there during resorting, this just modifies the sorting comparator used to sort any primary files at the start. This is slightly more generic than needed, also supporting multiple primary files, to at least not break the Comparator preconditions when for some reason there are multiple primary files.
1 parent 052764f commit 055cfc8

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

arduino-core/src/processing/app/SketchData.java

+4-19
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public class SketchData {
4545
private static final Comparator<SketchCode> CODE_DOCS_COMPARATOR = new Comparator<SketchCode>() {
4646
@Override
4747
public int compare(SketchCode x, SketchCode y) {
48+
if (x.isPrimary() && !y.isPrimary())
49+
return -1;
50+
if (y.isPrimary() && !x.isPrimary())
51+
return 1;
4852
return x.getFileName().compareTo(y.getFileName());
4953
}
5054
};
@@ -146,16 +150,6 @@ protected void load() throws IOException {
146150
if (getCodeCount() == 0)
147151
throw new IOException(tr("No valid code files found"));
148152

149-
// move the main class to the first tab
150-
// start at 1, if it's at zero, don't bother
151-
for (SketchCode code : getCodes()) {
152-
//if (code[i].file.getName().equals(mainFilename)) {
153-
if (code.getFile().equals(primaryFile)) {
154-
moveCodeToFront(code);
155-
break;
156-
}
157-
}
158-
159153
// sort the entries at the top
160154
sortCode();
161155
}
@@ -201,11 +195,6 @@ public void addCode(SketchCode sketchCode) {
201195
codes.add(sketchCode);
202196
}
203197

204-
public void moveCodeToFront(SketchCode codeDoc) {
205-
codes.remove(codeDoc);
206-
codes.add(0, codeDoc);
207-
}
208-
209198
protected void replaceCode(SketchCode newCode) {
210199
for (SketchCode code : codes) {
211200
if (code.getFileName().equals(newCode.getFileName())) {
@@ -216,11 +205,7 @@ protected void replaceCode(SketchCode newCode) {
216205
}
217206

218207
protected void sortCode() {
219-
if (codes.size() < 2)
220-
return;
221-
SketchCode first = codes.remove(0);
222208
Collections.sort(codes, CODE_DOCS_COMPARATOR);
223-
codes.add(0, first);
224209
}
225210

226211
public SketchCode getCode(int i) {

0 commit comments

Comments
 (0)