lua code formatting

This commit is contained in:
rusefillc 2021-11-25 10:18:50 -05:00
parent b06c4b4242
commit 0607658373
3 changed files with 98 additions and 1 deletions

View File

@ -6,7 +6,7 @@ import java.net.URL;
import java.util.concurrent.atomic.AtomicReference;
public class rusEFIVersion {
public static final int CONSOLE_VERSION = 20211124;
public static final int CONSOLE_VERSION = 20211125;
public static AtomicReference<String> firmwareVersion = new AtomicReference<>("N/A");
public static long classBuildTimeMillis() {

View File

@ -10,9 +10,11 @@ import com.rusefi.ui.UIContext;
import com.rusefi.ui.storage.Node;
import com.rusefi.ui.util.URLLabel;
import com.rusefi.ui.widgets.AnyCommand;
import neoe.formatter.lua.LuaFormatter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
@ -38,6 +40,7 @@ public class LuaScriptPanel {
JButton readButton = new JButton("Read from ECU");
JButton writeButton = new JButton("Write to ECU");
JButton resetButton = new JButton("Reset/Reload Lua");
JButton formatButton = new JButton("Format");
MessagesPanel mp = new MessagesPanel(null, config);
@ -49,7 +52,20 @@ public class LuaScriptPanel {
});
resetButton.addActionListener(e -> resetLua());
formatButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String sourceCode = scriptText.getText();
try {
String formatted = new LuaFormatter().format(sourceCode, new LuaFormatter.Env());
scriptText.setText(formatted);
} catch (Exception ignored) {
// todo: fix luaformatter no reason for exception
} }
});
upperPanel.add(readButton);
upperPanel.add(formatButton);
upperPanel.add(writeButton);
upperPanel.add(resetButton);
upperPanel.add(command.getContent());

View File

@ -0,0 +1,81 @@
package com.rusefi.ui;
import neoe.formatter.lua.LuaFormatter;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class LuaFormatterTest {
@Test
public void test() throws Exception {
String code = "function onCanRx(bus, id, dlc, data)\n" +
" --print('got CAN id=' .. id .. ' dlc=' .. dlc)\n" +
" id11=id%2048\n" +
"\n" +
"if id11 == 0x500 then --Check can state of BCM\n" +
" canState = data[1]\n" +
"if canState == 01 then\n" +
" packet502[1] = 0x01\n" +
"else\n" +
" packet502[1] = 0x00\n" +
"end\n" +
"if id11 == 0x570 then\n" +
" curState = data[1]\n" +
"\n" +
"if curState == 06 then -- Cranking TODO: MUST ONLY DO THIS ON RPM TILL STARt\n" +
"packet542[2] = 0x82\n" +
"end\n" +
"\n" +
"if curState == 04 then -- Kill off\n" +
"packet542[2] = 0x82\n" +
"end\n" +
"\n" +
"if curState == 01 then -- Kill\n" +
"packet542[2] = 0xA2\n" +
"end\n" +
"end\n" +
"end\n" +
"\t\n" +
"\n" +
"\n" +
"end";
String formatted = new LuaFormatter().format(code, new LuaFormatter.Env());
assertEquals("function onCanRx(bus, id, dlc, data)\n" +
"\t-- print('got CAN id=' .. id .. ' dlc=' .. dlc)\n" +
"\tid11 = id % 2048\n" +
"\n" +
"\tif id11 == 0x500 then\n" +
"\t\t-- Check can state of BCM\n" +
"\t\tcanState = data[1]\n" +
"\t\tif canState == 01 then\n" +
"\t\t\tpacket502[1] = 0x01\n" +
"\t\telse\n" +
"\t\t\tpacket502[1] = 0x00\n" +
"\t\tend\n" +
"\t\tif id11 == 0x570 then\n" +
"\t\t\tcurState = data[1]\n" +
"\n" +
"\t\t\tif curState == 06 then\n" +
"\t\t\t\t-- Cranking TODO: MUST ONLY DO THIS ON RPM TILL STARt\n" +
"\t\t\t\tpacket542[2] = 0x82\n" +
"\t\t\tend\n" +
"\n" +
"\t\t\tif curState == 04 then\n" +
"\t\t\t\t-- Kill off\n" +
"\t\t\t\tpacket542[2] = 0x82\n" +
"\t\t\tend\n" +
"\n" +
"\t\t\tif curState == 01 then\n" +
"\t\t\t\t-- Kill\n" +
"\t\t\t\tpacket542[2] = 0xA2\n" +
"\t\t\tend\n" +
"\t\tend\n" +
"\tend\n" +
"\n" +
"\n" +
"\n" +
"end\n" +
"\n", formatted);
}
}