Skip to content

Commit

Permalink
Added support for external programs
Browse files Browse the repository at this point in the history
  • Loading branch information
maccasoft committed Apr 23, 2024
1 parent 88d8176 commit bd74bfc
Show file tree
Hide file tree
Showing 9 changed files with 700 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.maccasoft.propeller.Preferences.ExternalTool;
import com.maccasoft.propeller.Preferences.SerializedPreferences;
import com.maccasoft.propeller.model.MethodNode;

Expand Down Expand Up @@ -497,4 +498,53 @@ void testSpin2Defines() throws Exception {
+ "}", os.toString().replaceAll("\\r\\n", "\n"));
}

@Test
void testSetExternalTools() throws Exception {
Preferences subject = new Preferences();

ExternalTool tool = new ExternalTool("flexspin", "/usr/bin/flexpin", "-2");
subject.setExternalTools(new ExternalTool[] {
tool
});

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"
+ " \"externalTools\" : [ {\n"
+ " \"name\" : \"flexspin\",\n"
+ " \"program\" : \"/usr/bin/flexpin\",\n"
+ " \"arguments\" : \"-2\"\n"
+ " } ]\n"
+ "}", os.toString().replaceAll("\\r\\n", "\n"));
}

@Test
void testGetExternalTools() throws Exception {
Preferences subject = new Preferences();
StringReader is = new StringReader(""
+ "{\n"
+ " \"externalTools\" : [ {\n"
+ " \"name\" : \"flexspin\",\n"
+ " \"program\" : \"/usr/bin/flexpin\",\n"
+ " \"arguments\" : \"-2\"\n"
+ " } ]\n"
+ "}");

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
subject.preferences = mapper.readValue(is, SerializedPreferences.class);

ExternalTool[] tools = subject.getExternalTools();

Assertions.assertEquals(1, tools.length);
Assertions.assertEquals("flexspin", tools[0].name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,23 @@ void testSkipLeadingAndTrailingSpaces() {
Assertions.assertEquals("%01_0100", Utils.makeSkipPattern(subject));
}

@Test
void testSplitArguments() {
String[] result = Utils.splitArguments("-2 -L/home/marco/lib source.spin2");
Assertions.assertEquals(3, result.length);
Assertions.assertEquals("-2", result[0]);
Assertions.assertEquals("-L/home/marco/lib", result[1]);
Assertions.assertEquals("source.spin2", result[2]);
}

@Test
void testSplitStringDelimitedArguments() {
String[] result = Utils.splitArguments("-2 -L \"/home/marco/lib\" source.spin2");
Assertions.assertEquals(4, result.length);
Assertions.assertEquals("-2", result[0]);
Assertions.assertEquals("-L", result[1]);
Assertions.assertEquals("/home/marco/lib", result[2]);
Assertions.assertEquals("source.spin2", result[3]);
}

}
32 changes: 17 additions & 15 deletions modules/spin-tools/src/com/maccasoft/propeller/ConsoleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ else if ((b & 0b11111_000) == 0b11110_000) {
utf_buf[utf_buf_index++] = b;
utf_ch_count = 4;
}
else if (b == '\n') {
append(lineBuilder.toString());
lineBuilder = new StringBuilder();
else if (b == '\n' || b == '\r') {
if (lineBuilder.length() != 0) {
append(lineBuilder.toString());
lineBuilder = new StringBuilder();
}
}
else if (b != '\r') {
else {
lineBuilder.append((char) b);
}
}
Expand Down Expand Up @@ -193,11 +195,13 @@ else if ((b & 0b11111_000) == 0b11110_000) {
utf_buf[utf_buf_index++] = (byte) b;
utf_ch_count = 4;
}
else if (b == '\n') {
append(lineBuilder.toString());
lineBuilder = new StringBuilder();
else if (b == '\n' || b == '\r') {
if (lineBuilder.length() != 0) {
append(lineBuilder.toString());
lineBuilder = new StringBuilder();
}
}
else if (b != '\r') {
else {
lineBuilder.append((char) b);
}
}
Expand All @@ -210,23 +214,21 @@ else if (b != '\r') {
}
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
write(b);
}

@Override
public void flush() throws IOException {

}

@Override
public void close() throws IOException {
flush();
if (lineBuilder.length() != 0) {
append(lineBuilder.toString());
lineBuilder = new StringBuilder();
}
}

private void append(String text) {
display.asyncExec(new Runnable() {
display.syncExec(new Runnable() {

@Override
public void run() {
Expand Down
186 changes: 186 additions & 0 deletions modules/spin-tools/src/com/maccasoft/propeller/ExternalToolDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (c) 2021-24 Marco Maccaferri and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package com.maccasoft.propeller;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.maccasoft.propeller.Preferences.ExternalTool;
import com.maccasoft.propeller.internal.ImageRegistry;

public class ExternalToolDialog extends Dialog {

Text name;
Button browse;
Text program;
Text arguments;

ExternalTool externalTool;

ModifyListener textModifyListener = new ModifyListener() {

@Override
public void modifyText(ModifyEvent e) {
validate();
}
};

FocusListener textFocusListener = new FocusAdapter() {

@Override
public void focusGained(FocusEvent e) {
((Text) e.widget).selectAll();
}

};

public ExternalToolDialog(Shell parentShell) {
super(parentShell);
}

public ExternalToolDialog(Shell parentShell, ExternalTool externalTool) {
super(parentShell);
this.externalTool = externalTool;
}

@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("External Tool");
}

@Override
protected Control createDialogArea(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
composite.setLayout(layout);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setBackgroundMode(SWT.INHERIT_DEFAULT);
applyDialogFont(composite);

Label label = new Label(composite, SWT.NONE);
label.setText("Name");
name = new Text(composite, SWT.BORDER);
name.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
name.addFocusListener(textFocusListener);

label = new Label(composite, SWT.NONE);
label.setText("Program");
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

Composite group = new Composite(composite, SWT.NONE);
layout = new GridLayout(2, false);
layout.marginWidth = layout.marginHeight = 0;
group.setLayout(layout);
group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

program = new Text(group, SWT.BORDER);
program.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
((GridData) program.getLayoutData()).widthHint = convertWidthInCharsToPixels(55);
program.addFocusListener(textFocusListener);

browse = new Button(group, SWT.PUSH);
browse.setImage(ImageRegistry.getImageFromResources("folder-horizontal-open.png"));
browse.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
FileDialog dlg = new FileDialog(group.getShell(), SWT.OPEN);
String fileName = dlg.open();
if (fileName != null) {
program.setText(fileName);
}
}

});

label = new Label(composite, SWT.NONE);
label.setText("Arguments");
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
arguments = new Text(composite, SWT.MULTI | SWT.BORDER);
arguments.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
((GridData) arguments.getLayoutData()).heightHint = convertHeightInCharsToPixels(5);
arguments.addFocusListener(textFocusListener);

label = new Label(composite, SWT.NONE);
label.setText("${file} insert the currently selected editor's file or pinned top file.");
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));

if (externalTool != null) {
name.setText(externalTool.getName());
program.setText(externalTool.getProgram());
arguments.setText(externalTool.getArguments());
}

name.addModifyListener(textModifyListener);
program.addModifyListener(textModifyListener);
arguments.addModifyListener(textModifyListener);

return composite;
}

@Override
public void create() {
super.create();
validate();
}

void validate() {
boolean ok = true;

if (name.getText().trim().isEmpty()) {
ok = false;
}
if (program.getText().trim().isEmpty()) {
ok = false;
}

getButton(OK).setEnabled(ok);
}

@Override
protected void okPressed() {
if (externalTool == null) {
externalTool = new ExternalTool(name.getText(), program.getText(), arguments.getText());
}
else {
externalTool.setName(name.getText());
externalTool.setProgram(program.getText());
externalTool.setArguments(arguments.getText());
}
super.okPressed();
}

public ExternalTool getExternalTool() {
return externalTool;
}

}
Loading

0 comments on commit bd74bfc

Please sign in to comment.