diff --git a/modules/spin-tools/src-tests/com/maccasoft/propeller/PreferencesTest.java b/modules/spin-tools/src-tests/com/maccasoft/propeller/PreferencesTest.java index da8f37df..9a8cc1b9 100644 --- a/modules/spin-tools/src-tests/com/maccasoft/propeller/PreferencesTest.java +++ b/modules/spin-tools/src-tests/com/maccasoft/propeller/PreferencesTest.java @@ -15,6 +15,8 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; @@ -443,4 +445,56 @@ public void propertyChange(PropertyChangeEvent evt) { Assertions.assertArrayEquals(data, subject.preferences.roots); } + @Test + void testSpin1Defines() throws Exception { + Preferences subject = new Preferences(); + + Map defines = new HashMap<>(); + defines.put("KEY", "VALUE"); + defines.put("EMPTY_KEY", ""); + subject.setSpin1Defines(defines); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(SerializationFeature.INDENT_OUTPUT, true); + mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); + mapper.setSerializationInclusion(Include.NON_DEFAULT); + mapper.writeValue(os, subject.preferences); + + Assertions.assertEquals("" + + "{\n" + + " \"spin1Defines\" : {\n" + + " \"EMPTY_KEY\" : \"\",\n" + + " \"KEY\" : \"VALUE\"\n" + + " }\n" + + "}", os.toString().replaceAll("\\r\\n", "\n")); + } + + @Test + void testSpin2Defines() throws Exception { + Preferences subject = new Preferences(); + + Map defines = new HashMap<>(); + defines.put("KEY", "VALUE"); + defines.put("EMPTY_KEY", ""); + subject.setSpin2Defines(defines); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(SerializationFeature.INDENT_OUTPUT, true); + mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); + mapper.setSerializationInclusion(Include.NON_DEFAULT); + mapper.writeValue(os, subject.preferences); + + Assertions.assertEquals("" + + "{\n" + + " \"spin2Defines\" : {\n" + + " \"EMPTY_KEY\" : \"\",\n" + + " \"KEY\" : \"VALUE\"\n" + + " }\n" + + "}", os.toString().replaceAll("\\r\\n", "\n")); + } + } diff --git a/modules/spin-tools/src/com/maccasoft/propeller/EditorTab.java b/modules/spin-tools/src/com/maccasoft/propeller/EditorTab.java index 5d10de26..27beeb0c 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/EditorTab.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/EditorTab.java @@ -19,6 +19,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; @@ -57,13 +58,16 @@ import com.maccasoft.propeller.model.ObjectsNode; import com.maccasoft.propeller.model.SourceProvider; import com.maccasoft.propeller.model.Token; +import com.maccasoft.propeller.model.TokenStream; import com.maccasoft.propeller.model.VariablesNode; import com.maccasoft.propeller.spin1.Spin1Compiler; import com.maccasoft.propeller.spin1.Spin1Formatter; import com.maccasoft.propeller.spin1.Spin1TokenMarker; +import com.maccasoft.propeller.spin1.Spin1TokenStream; import com.maccasoft.propeller.spin2.Spin2Compiler; import com.maccasoft.propeller.spin2.Spin2Formatter; import com.maccasoft.propeller.spin2.Spin2TokenMarker; +import com.maccasoft.propeller.spin2.Spin2TokenStream; import com.maccasoft.propeller.spinc.CTokenMarker; import com.maccasoft.propeller.spinc.Spin1CCompiler; import com.maccasoft.propeller.spinc.Spin2CCompiler; @@ -493,10 +497,30 @@ public void run() { if (tabItemText.toLowerCase().endsWith(".spin")) { compiler = new Spin1Compiler(preferences.getSpin1CaseSensitiveSymbols(), true); compiler.setSourceProvider(new EditorTabSourceProvider(preferences.getSpin1LibraryPath())); + + for (Entry entry : preferences.getSpin1Defines().entrySet()) { + Token token; + TokenStream stream = new Spin1TokenStream(entry.getValue()); + List list = new ArrayList<>(); + while ((token = stream.nextToken()).type != Token.EOF) { + list.add(token); + } + compiler.addDefine(entry.getKey(), list); + } } else if (tabItemText.toLowerCase().endsWith(".spin2")) { compiler = new Spin2Compiler(preferences.getSpin2CaseSensitiveSymbols()); compiler.setSourceProvider(new EditorTabSourceProvider(preferences.getSpin2LibraryPath())); + + for (Entry entry : preferences.getSpin2Defines().entrySet()) { + Token token; + TokenStream stream = new Spin2TokenStream(entry.getValue()); + List list = new ArrayList<>(); + while ((token = stream.nextToken()).type != Token.EOF) { + list.add(token); + } + compiler.addDefine(entry.getKey(), list); + } } else if (tabItemText.toLowerCase().endsWith(".c")) { for (Node node : root.getChilds()) { diff --git a/modules/spin-tools/src/com/maccasoft/propeller/Preferences.java b/modules/spin-tools/src/com/maccasoft/propeller/Preferences.java index 69b21358..08119304 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/Preferences.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/Preferences.java @@ -177,11 +177,15 @@ public SerializedPreferences() { public String[] spin1LibraryPath; public boolean spin1CaseSensitiveSymbols; public String spin1Template; + @JsonInclude(Include.NON_ABSENT) + public Map spin1Defines; public String[] spin2LibraryPath; public boolean spin2CaseSensitiveSymbols; public boolean spin2ClockSetter; public String spin2Template; + @JsonInclude(Include.NON_ABSENT) + public Map spin2Defines; public List lru; @@ -581,6 +585,22 @@ public void setSpin1CaseSensitiveSymbols(boolean spin1CaseSensitiveSymbols) { changeSupport.firePropertyChange(PROP_SPIN1_CASE_SENSITIVE_SYMBOLS, preferences.spin1CaseSensitiveSymbols, preferences.spin1CaseSensitiveSymbols = spin1CaseSensitiveSymbols); } + public Map getSpin1Defines() { + return preferences.spin1Defines != null ? preferences.spin1Defines : new HashMap<>(); + } + + public void setSpin1Defines(Map spin1Defines) { + if (spin1Defines.isEmpty()) { + preferences.spin1Defines = null; + } + else { + if (preferences.spin1Defines == null) { + preferences.spin1Defines = new HashMap<>(); + } + preferences.spin1Defines.putAll(spin1Defines); + } + } + public File[] getSpin2LibraryPath() { if (preferences.spin2LibraryPath != null) { List l = new ArrayList<>(); @@ -634,6 +654,22 @@ public void setSpin2Template(File spin2Template) { preferences.spin2Template = spin2Template != null ? spin2Template.getAbsolutePath() : null; } + public Map getSpin2Defines() { + return preferences.spin2Defines != null ? preferences.spin2Defines : new HashMap<>(); + } + + public void setSpin2Defines(Map spin2Defines) { + if (spin2Defines.isEmpty()) { + preferences.spin2Defines = null; + } + else { + if (preferences.spin2Defines == null) { + preferences.spin2Defines = new HashMap<>(); + } + preferences.spin2Defines.putAll(spin2Defines); + } + } + public int[] getTabStops(Class clazz) { int[] result = null;