|
9 | 9 |
|
10 | 10 | import cc.arduino.files.DeleteFilesOnShutdown;
|
11 | 11 | import processing.app.helpers.FileUtils;
|
12 |
| -import processing.app.EditorTab; |
13 | 12 |
|
14 | 13 | import static processing.app.I18n.tr;
|
15 | 14 |
|
@@ -166,24 +165,23 @@ private static int ordinalIndexOf(String str, String substr, int n) {
|
166 | 165 | return pos;
|
167 | 166 | }
|
168 | 167 |
|
169 |
| - private void removeBuildSettingsHeader(EditorTab tab){ |
170 |
| - if(tab.getText().contains(buildToolsHeader)) { |
171 |
| - int headerStartIndex = tab.getText().indexOf(buildToolsHeader); |
172 |
| - int headerStopIndex = tab.getText().indexOf(buildToolsHeaderEnd); |
| 168 | + private String removeBuildSettingsHeader(Sketch sketch){ |
| 169 | + if(sketch.getPrimaryFile().getProgram().contains(buildToolsHeader)) { |
| 170 | + int headerStartIndex = sketch.getPrimaryFile().getProgram().indexOf(buildToolsHeader); |
| 171 | + int headerStopIndex = sketch.getPrimaryFile().getProgram().indexOf(buildToolsHeaderEnd); |
173 | 172 | if (headerStartIndex > headerStopIndex) {
|
174 | 173 | System.err.println("The build tool header is not the first comment block in your file! Please fix this.");
|
175 |
| - for (int i = 0; i < tab.getText().length(); i++) { |
176 |
| - if (headerStartIndex < ordinalIndexOf(tab.getText(), buildToolsHeaderEnd, i)) { |
177 |
| - headerStopIndex = ordinalIndexOf(tab.getText(), buildToolsHeaderEnd, i); |
| 174 | + for (int i = 0; i < sketch.getPrimaryFile().getProgram().length(); i++) { |
| 175 | + if (headerStartIndex < ordinalIndexOf(sketch.getPrimaryFile().getProgram(), buildToolsHeaderEnd, i)) { |
| 176 | + headerStopIndex = ordinalIndexOf(sketch.getPrimaryFile().getProgram(), buildToolsHeaderEnd, i); |
178 | 177 | break;
|
179 | 178 | }
|
180 | 179 | }
|
181 | 180 | }
|
182 |
| - String header = tab.getText().substring(headerStartIndex, headerStopIndex + buildToolsHeaderEnd.length()); |
183 |
| - tab.setText(tab.getText().replace(header, "")); |
184 |
| - // Run this method again so we are sure that there aren't any headers left |
185 |
| - removeBuildSettingsHeader(tab); |
| 181 | + String header = sketch.getPrimaryFile().getProgram().substring(headerStartIndex, headerStopIndex + buildToolsHeaderEnd.length()); |
| 182 | + return sketch.getPrimaryFile().getProgram().replace(header, ""); |
186 | 183 | }
|
| 184 | + return sketch.getPrimaryFile().getProgram(); |
187 | 185 | }
|
188 | 186 |
|
189 | 187 | /**
|
@@ -233,16 +231,17 @@ private boolean isBuildSettingsEqual(LinkedHashMap<String,String> first, LinkedH
|
233 | 231 | return first.keySet().containsAll(second.keySet()) && first.values().containsAll(second.values());
|
234 | 232 | }
|
235 | 233 |
|
236 |
| - public void setBuildSettings(EditorTab tab, LinkedHashMap<String, String> buildSettings){ |
237 |
| - if(tab.getSketch().getSketch() != this){ |
238 |
| - return; |
| 234 | + public String setBuildSettings(Sketch sketch, LinkedHashMap<String, String> buildSettings){ |
| 235 | + if(sketch != this){ |
| 236 | + return ""; |
239 | 237 | }
|
240 | 238 |
|
241 | 239 | String customBoardSettingsHeader = buildSettings.entrySet().stream().map(entry-> String.format(" * %s: %s\n", entry.getKey(), entry.getValue())).collect(Collectors.joining("", buildToolsHeader, "*/"));
|
242 |
| - if(!isBuildSettingsEqual(getBuildSettingsFromProgram(tab.getText()),buildSettings)){ |
243 |
| - removeBuildSettingsHeader(tab); |
244 |
| - tab.setText(customBoardSettingsHeader + ((tab.getText().charAt(0) == '\n') ? "" : "\n") + tab.getText()); |
| 240 | + if(!isBuildSettingsEqual(getBuildSettingsFromProgram(sketch.getPrimaryFile().getProgram()),buildSettings)){ |
| 241 | + String headerLessProgram = removeBuildSettingsHeader(sketch); |
| 242 | + return customBoardSettingsHeader + ((headerLessProgram.charAt(0) == '\n') ? "" : "\n") + headerLessProgram; |
245 | 243 | }
|
| 244 | + return ""; |
246 | 245 | }
|
247 | 246 |
|
248 | 247 | public int getCodeCount() {
|
|
0 commit comments