diff --git a/modules/spin-tools/src-tests/com/maccasoft/propeller/internal/UtilsTest.java b/modules/spin-tools/src-tests/com/maccasoft/propeller/internal/UtilsTest.java new file mode 100644 index 00000000..8b0fd5d6 --- /dev/null +++ b/modules/spin-tools/src-tests/com/maccasoft/propeller/internal/UtilsTest.java @@ -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)); + } + +} diff --git a/modules/spin-tools/src/com/maccasoft/propeller/SourceEditor.java b/modules/spin-tools/src/com/maccasoft/propeller/SourceEditor.java index bf7201b6..50988788 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/SourceEditor.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/SourceEditor.java @@ -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; @@ -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; @@ -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; @@ -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); + } + } diff --git a/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java b/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java index 773b97b6..12c380da 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java @@ -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); diff --git a/modules/spin-tools/src/com/maccasoft/propeller/internal/Utils.java b/modules/spin-tools/src/com/maccasoft/propeller/internal/Utils.java new file mode 100644 index 00000000..3b395065 --- /dev/null +++ b/modules/spin-tools/src/com/maccasoft/propeller/internal/Utils.java @@ -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(); + } + +}