Skip to content

Commit 37a61e1

Browse files
authored
Merge branch 'main' into issue-156-remove-ads
2 parents b75e606 + c50858b commit 37a61e1

File tree

16 files changed

+4829
-2550
lines changed

16 files changed

+4829
-2550
lines changed

client/src/com/mirth/connect/client/ui/components/rsta/MirthRSyntaxTextArea.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.mirth.connect.client.ui.components.rsta.actions.ExpandFoldAction;
6363
import com.mirth.connect.client.ui.components.rsta.actions.FindNextAction;
6464
import com.mirth.connect.client.ui.components.rsta.actions.FindReplaceAction;
65+
import com.mirth.connect.client.ui.components.rsta.actions.FormatCodeAction;
6566
import com.mirth.connect.client.ui.components.rsta.actions.GoToMatchingBracketAction;
6667
import com.mirth.connect.client.ui.components.rsta.actions.HorizontalPageAction;
6768
import com.mirth.connect.client.ui.components.rsta.actions.InsertBreakAction;
@@ -127,6 +128,9 @@ public class MirthRSyntaxTextArea extends RSyntaxTextArea implements MirthTextIn
127128
private CustomJCheckBoxMenuItem showWhitespaceMenuItem;
128129
private CustomJCheckBoxMenuItem showLineEndingsMenuItem;
129130
private CustomJCheckBoxMenuItem wrapLinesMenuItem;
131+
private JMenu codeMenu;
132+
private CustomMenuItem formatCodeMenuItem;
133+
private CustomMenuItem toggleCommentMenuItem;
130134
private JMenu macroMenu;
131135
private CustomMenuItem beginMacroMenuItem;
132136
private CustomMenuItem endMacroMenuItem;
@@ -228,6 +232,9 @@ public void keyPressed(KeyEvent e) {
228232
showWhitespaceMenuItem = new CustomJCheckBoxMenuItem(this, new ShowWhitespaceAction(this), ActionInfo.DISPLAY_SHOW_WHITESPACE);
229233
showLineEndingsMenuItem = new CustomJCheckBoxMenuItem(this, new ShowLineEndingsAction(this), ActionInfo.DISPLAY_SHOW_LINE_ENDINGS);
230234
wrapLinesMenuItem = new CustomJCheckBoxMenuItem(this, new WrapLinesAction(this), ActionInfo.DISPLAY_WRAP_LINES);
235+
codeMenu = new JMenu("Code");
236+
formatCodeMenuItem = new CustomMenuItem(this, new FormatCodeAction(this), ActionInfo.FORMAT_CODE);
237+
toggleCommentMenuItem = new CustomMenuItem(this, new ToggleCommentAction(this), ActionInfo.TOGGLE_COMMENT);
231238
macroMenu = new JMenu("Macro");
232239
beginMacroMenuItem = new CustomMenuItem(this, new BeginMacroAction(), ActionInfo.MACRO_BEGIN);
233240
endMacroMenuItem = new CustomMenuItem(this, new EndMacroAction(), ActionInfo.MACRO_END);
@@ -239,7 +246,8 @@ public void keyPressed(KeyEvent e) {
239246
getActionMap().put(ActionInfo.DELETE_LINE.getActionMapKey(), new DeleteLineAction());
240247
getActionMap().put(ActionInfo.JOIN_LINE.getActionMapKey(), new JoinLineAction());
241248
getActionMap().put(ActionInfo.GO_TO_MATCHING_BRACKET.getActionMapKey(), new GoToMatchingBracketAction());
242-
getActionMap().put(ActionInfo.TOGGLE_COMMENT.getActionMapKey(), new ToggleCommentAction());
249+
getActionMap().put(ActionInfo.FORMAT_CODE.getActionMapKey(), new FormatCodeAction(this));
250+
getActionMap().put(ActionInfo.TOGGLE_COMMENT.getActionMapKey(), new ToggleCommentAction(this));
243251
getActionMap().put(ActionInfo.DOCUMENT_START.getActionMapKey(), new DocumentStartAction(false));
244252
getActionMap().put(ActionInfo.DOCUMENT_SELECT_START.getActionMapKey(), new DocumentStartAction(true));
245253
getActionMap().put(ActionInfo.DOCUMENT_END.getActionMapKey(), new DocumentEndAction(false));
@@ -489,6 +497,11 @@ protected JPopupMenu createPopupMenu() {
489497
menu.add(displayMenu);
490498
menu.addSeparator();
491499

500+
codeMenu.add(formatCodeMenuItem);
501+
codeMenu.add(toggleCommentMenuItem);
502+
menu.add(codeMenu);
503+
menu.addSeparator();
504+
492505
macroMenu.add(beginMacroMenuItem);
493506
macroMenu.add(endMacroMenuItem);
494507
macroMenu.add(playbackMacroMenuItem);
@@ -514,6 +527,8 @@ protected void configurePopupMenu(JPopupMenu popupMenu) {
514527
findNextMenuItem.setEnabled(findNextMenuItem.getAction().isEnabled() && CollectionUtils.isNotEmpty(rstaPreferences.getFindReplaceProperties().getFindHistory()));
515528
clearMarkedOccurrencesMenuItem.setEnabled(clearMarkedOccurrencesMenuItem.getAction().isEnabled() && canType && ((RSyntaxTextAreaHighlighter) getHighlighter()).getMarkAllHighlightCount() > 0);
516529
foldingMenu.setEnabled(getFoldManager().isCodeFoldingSupportedAndEnabled());
530+
formatCodeMenuItem.setEnabled(formatCodeMenuItem.getAction().isEnabled());
531+
toggleCommentMenuItem.setEnabled(toggleCommentMenuItem.getAction().isEnabled());
517532
beginMacroMenuItem.setEnabled(!isRecordingMacro());
518533
endMacroMenuItem.setEnabled(isRecordingMacro());
519534
playbackMacroMenuItem.setEnabled(!isRecordingMacro() && getCurrentMacro() != null);
@@ -533,6 +548,8 @@ protected void configurePopupMenu(JPopupMenu popupMenu) {
533548
collapseAllFoldsMenuItem.updateAccelerator();
534549
collapseAllCommentFoldsMenuItem.updateAccelerator();
535550
expandAllFoldsMenuItem.updateAccelerator();
551+
formatCodeMenuItem.updateAccelerator();
552+
toggleCommentMenuItem.updateAccelerator();
536553
beginMacroMenuItem.updateAccelerator();
537554
endMacroMenuItem.updateAccelerator();
538555
playbackMacroMenuItem.updateAccelerator();
@@ -550,4 +567,4 @@ public void setSyntaxEditingStyle(String styleKey) {
550567
public Action[] getActions() {
551568
return actions != null ? actions : getUI().getEditorKit(this).getActions();
552569
}
553-
}
570+
}

client/src/com/mirth/connect/client/ui/components/rsta/MirthRSyntaxTextArea.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ TOGGLE_COMMENT.Name=Toggle Comment
118118
TOGGLE_COMMENT.Desc=Comments / uncomments the current selected line(s).
119119
TOGGLE_COMMENT.Toggle=false
120120

121+
FORMAT_CODE.Mnemonic=F
122+
FORMAT_CODE.Name=Format
123+
FORMAT_CODE.Desc=Format the current script / selected line(s).
124+
FORMAT_CODE.Toggle=false
125+
121126
VIEW_USER_API.Mnemonic=V
122127
VIEW_USER_API.Name=View User API
123128
VIEW_USER_API.Desc=Opens up the User API Javadoc in a browser.

client/src/com/mirth/connect/client/ui/components/rsta/RSTAPreferences.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ private void setDefaultKeyStrokeMap() {
234234
putKeyStroke(ActionInfo.FOLD_EXPAND_ALL, KeyEvent.VK_MULTIPLY, defaultModifier);
235235
putKeyStroke(ActionInfo.GO_TO_MATCHING_BRACKET, KeyEvent.VK_OPEN_BRACKET, defaultModifier);
236236
putKeyStroke(ActionInfo.TOGGLE_COMMENT, KeyEvent.VK_SLASH, defaultModifier);
237+
putKeyStroke(ActionInfo.FORMAT_CODE, KeyEvent.VK_F, ctrl + shift);
237238
putKeyStroke(ActionInfo.AUTO_COMPLETE, KeyEvent.VK_SPACE, ctrl);
238239

239240
if (isOSX) {

client/src/com/mirth/connect/client/ui/components/rsta/actions/ActionInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public enum ActionInfo {
4040
DISPLAY_WRAP_LINES ("mirth-wrap-lines"),
4141
GO_TO_MATCHING_BRACKET (RSyntaxTextAreaEditorKit.rstaGoToMatchingBracketAction),
4242
TOGGLE_COMMENT (RSyntaxTextAreaEditorKit.rstaToggleCommentAction),
43+
FORMAT_CODE ("mirth-format-code"),
4344
VIEW_USER_API ("mirth-view-user-api"),
4445
AUTO_COMPLETE ("AutoComplete"),
4546
DOCUMENT_START (DefaultEditorKit.beginAction),
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
// SPDX-FileCopyrightText: 2025 Simon Braconnier <[email protected]>
3+
4+
package com.mirth.connect.client.ui.components.rsta.actions;
5+
6+
import java.awt.event.ActionEvent;
7+
8+
import javax.swing.UIManager;
9+
import javax.swing.text.BadLocationException;
10+
import javax.swing.text.Caret;
11+
import javax.swing.text.Element;
12+
13+
import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
14+
15+
import com.mirth.connect.client.ui.components.rsta.MirthRSyntaxTextArea;
16+
import com.mirth.connect.util.JavaScriptSharedUtil;
17+
18+
public class FormatCodeAction extends MirthRecordableTextAction {
19+
20+
public FormatCodeAction(MirthRSyntaxTextArea textArea) {
21+
super(textArea, ActionInfo.FORMAT_CODE);
22+
}
23+
24+
@Override
25+
public void actionPerformedImpl(ActionEvent evt) {
26+
27+
if (!textArea.isEditable() || !textArea.isEnabled()) {
28+
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
29+
return;
30+
}
31+
32+
Caret c = textArea.getCaret();
33+
int start = Math.min(c.getDot(), c.getMark());
34+
int end = Math.max(c.getDot(), c.getMark());
35+
if (start == end) {
36+
formatAll();
37+
} else {
38+
formatRange(start, end);
39+
}
40+
41+
}
42+
43+
@Override
44+
public boolean isEnabled() {
45+
return textArea.isEditable() && textArea.isEnabled();
46+
}
47+
48+
private void formatAll() {
49+
50+
String code = textArea.getText();
51+
formatAndReplace(code, 0, code.length());
52+
}
53+
54+
private void formatRange(int start, int end) {
55+
56+
// We want to format all the lines of the selection (not only the selected text)
57+
58+
try {
59+
RSyntaxDocument doc = (RSyntaxDocument) textArea.getDocument();
60+
Element map = doc.getDefaultRootElement();
61+
62+
// Get the lines indexes from the selected text indexes.
63+
int startLine = map.getElementIndex(start);
64+
int endLine = map.getElementIndex(end);
65+
66+
int replaceRangeStart = 0;
67+
int replaceRangeEnd = 0;
68+
69+
// Build a string from the selected lines.
70+
StringBuilder sb = new StringBuilder();
71+
for (int line = startLine; line <= endLine; line++) {
72+
Element elem = map.getElement(line);
73+
74+
int startOffset = elem.getStartOffset();
75+
int endOffset = elem.getEndOffset() - 1;
76+
77+
// Save the start index of the first line for the final replacement.
78+
if (line == startLine) {
79+
replaceRangeStart = startOffset;
80+
}
81+
82+
// Save the end index of the last line for the final replacement.
83+
if (line == endLine) {
84+
replaceRangeEnd = endOffset;
85+
}
86+
87+
sb.append(doc.getText(startOffset, endOffset - startOffset + 1));
88+
}
89+
90+
// Format the code and replace the selected lines.
91+
formatAndReplace(sb.toString(), replaceRangeStart, replaceRangeEnd);
92+
93+
} catch (BadLocationException ble) {
94+
ble.printStackTrace();
95+
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
96+
}
97+
}
98+
99+
private void formatAndReplace(String code, int start, int end) {
100+
101+
String formattedCode = JavaScriptSharedUtil.prettyPrint(code);
102+
textArea.replaceRange(formattedCode, start, end);
103+
textArea.setCaretPosition(start);
104+
}
105+
}

client/src/com/mirth/connect/client/ui/components/rsta/actions/ToggleCommentAction.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@
1313

1414
public class ToggleCommentAction extends org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit.ToggleCommentAction {
1515

16-
public ToggleCommentAction() {
16+
protected MirthRSyntaxTextArea textArea;
17+
18+
public ToggleCommentAction(MirthRSyntaxTextArea textArea) {
1719
setProperties(MirthRSyntaxTextArea.getResourceBundle(), ActionInfo.TOGGLE_COMMENT.toString());
20+
21+
this.textArea = textArea;
22+
}
23+
24+
@Override
25+
public boolean isEnabled() {
26+
return textArea.isEditable() && textArea.isEnabled();
1827
}
1928
}

0 commit comments

Comments
 (0)