Skip to content

Commit

Permalink
Added experimental skip pattern generator
Browse files Browse the repository at this point in the history
  • Loading branch information
maccasoft committed Apr 18, 2024
1 parent 4e561f2 commit 3c007fd
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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
*
* Contributors:
* Marco Maccaferri - initial API and implementation
*/

package com.maccasoft.propeller.internal;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class UtilsTest {

@Test
void testSimplePattern() {
String subject = "" +
"a\n" +
"a\n" +
"|\n" +
"a\n" +
" \n" +
"|\n" +
"a\n" +
"";
Assertions.assertEquals("%01_0100", Utils.makeSkipPattern(subject));
}

@Test
void testSkipMultipleSpaces() {
String subject = "" +
"a\n" +
"a\n" +
"|\n" +
"a\n" +
" \n" +
" \n" +
" \n" +
"|\n" +
"a\n" +
"";
Assertions.assertEquals("%01_0100", Utils.makeSkipPattern(subject));
}

@Test
void testSkipLeadingAndTrailingSpaces() {
String subject = "" +
" \n" +
" \n" +
"a\n" +
"a\n" +
"|\n" +
"a\n" +
" \n" +
"|\n" +
"a\n" +
" \n" +
" \n" +
"";
Assertions.assertEquals("%01_0100", Utils.makeSkipPattern(subject));
}

}
29 changes: 29 additions & 0 deletions modules/spin-tools/src/com/maccasoft/propeller/SourceEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.jface.resource.JFaceResources;
Expand All @@ -41,6 +42,9 @@
import org.eclipse.swt.custom.TextChangedEvent;
import org.eclipse.swt.custom.TextChangingEvent;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
Expand Down Expand Up @@ -90,6 +94,7 @@
import com.maccasoft.propeller.internal.HTMLStyledTextDecorator;
import com.maccasoft.propeller.internal.IContentProposalListener2;
import com.maccasoft.propeller.internal.StyledTextContentAdapter;
import com.maccasoft.propeller.internal.Utils;
import com.maccasoft.propeller.model.ConstantNode;
import com.maccasoft.propeller.model.ConstantsNode;
import com.maccasoft.propeller.model.DataLineNode;
Expand Down Expand Up @@ -2581,4 +2586,28 @@ public void setIndentLinesForeground(Color color) {
styledText.redraw();
}

public void makeSkipPattern() {
String text = styledText.getSelectionText();
String pattern = Utils.makeSkipPattern(text);

if (pattern.length() == 0) {
MessageDialog.openWarning(styledText.getShell(), SpinTools.APP_TITLE, "No pattern detected");
return;
}

Clipboard clipboard = new Clipboard(display);
try {
clipboard.setContents(new Object[] {
pattern,
}, new Transfer[] {
TextTransfer.getInstance(),
});
} catch (Exception ex) {
ex.printStackTrace();
}
clipboard.dispose();

MessageDialog.openInformation(styledText.getShell(), SpinTools.APP_TITLE, "Skip pattern copied to clipboard:" + styledText.getLineDelimiter() + styledText.getLineDelimiter() + pattern);
}

}
16 changes: 16 additions & 0 deletions modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,22 @@ public void handleEvent(Event e) {
}
});

item = new MenuItem(menu, SWT.PUSH);
item.setText("Make skip pattern\tShift+Alt+P");
item.setAccelerator(SWT.MOD2 + SWT.MOD3 + 'P');
item.addListener(SWT.Selection, new Listener() {

@Override
public void handleEvent(Event e) {
CTabItem tabItem = tabFolder.getSelection();
if (tabItem == null) {
return;
}
EditorTab editorTab = (EditorTab) tabItem.getData();
editorTab.getEditor().makeSkipPattern();
}
});

new MenuItem(menu, SWT.SEPARATOR);

item = new MenuItem(menu, SWT.PUSH);
Expand Down
65 changes: 65 additions & 0 deletions modules/spin-tools/src/com/maccasoft/propeller/internal/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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
*
* Contributors:
* Marco Maccaferri - initial API and implementation
*/

package com.maccasoft.propeller.internal;

public class Utils {

public static String makeSkipPattern(String text) {
StringBuilder pattern = new StringBuilder();

String[] s = text.split(FileUtils.EOL_PATTERN);

char target = 0;
for (int i = 0; i < s.length; i++) {
if (s[i].length() < 1) {
if (pattern.length() == 0 || pattern.charAt(0) != '_') {
pattern.insert(0, '_');
}
continue;
}
char ch = s[i].charAt(0);
if (ch == '-' || ch == '_') {
pattern.insert(0, ch);
}
else if (ch == '|') {
pattern.insert(0, '1');
}
else {
if (target == 0) {
if (ch == ' ') {
continue;
}
target = ch;
}
if (ch == target) {
pattern.insert(0, '0');
}
else {
if (pattern.length() == 0 || pattern.charAt(0) != '_') {
pattern.insert(0, '_');
}
}
}
}

while (pattern.length() >= 1 && pattern.charAt(0) == '_') {
pattern.deleteCharAt(0);
}

if (pattern.length() > 0) {
pattern.insert(0, '%');
}

return pattern.toString();
}

}

0 comments on commit 3c007fd

Please sign in to comment.