Skip to content

Commit

Permalink
implemented un/wrap auto-detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kstenschke committed Jul 18, 2014
1 parent 7f2fd7f commit 6e224bf
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 43 deletions.
6 changes: 3 additions & 3 deletions META-INF/plugin.xml
Expand Up @@ -37,14 +37,14 @@
<br/>Please note: This plugin is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
]]></description>
<vendor url="" email="info@stenschke.com">Kay Stenschke</vendor>
<version>1.2.4</version>
<version>1.3.0</version>
<idea-version since-build="8000"/>

<change-notes><![CDATA[
<ul>
<li>1.2.4
<li>1.3.0
<ul>
<li>Added autodetect quick-wrap mode option</li>
<li>Added autodetect: unwrap (if already wrapped) or wrap?</li>
<li>Added retina icons</li>
</ul>
</li>
Expand Down
Binary file modified realigner-plugin.jar
Binary file not shown.
5 changes: 3 additions & 2 deletions src/com/kstenschke/realigner/actions/WrapAction.java
Expand Up @@ -65,11 +65,12 @@ public void run() {

Preferences.saveWrapProperties(prefix, postfix);

int operation = optionsDialog.clickedOperation;
if( optionsDialog.clickedOperation == DialogWrapOptions.OPERATION_AUTODETECT ) {
// todo implement mode detection
operation = wrapper.isWrapped(prefix, postfix) ? DialogWrapOptions.OPERATION_UNWRAP : DialogWrapOptions.OPERATION_WRAP;
}
// Perform actual wrap or unwrap
switch( optionsDialog.clickedOperation ) {
switch( operation ) {
case DialogWrapOptions.OPERATION_WRAP:
CommandProcessor.getInstance().executeCommand(currentProject, new Runnable() {
public void run() {
Expand Down
79 changes: 58 additions & 21 deletions src/com/kstenschke/realigner/actions/Wrapper.java
Expand Up @@ -71,6 +71,24 @@ void initSelectionProperties() {
}
}

/**
* @return int Line number where the caret is
*/
private int getCaretLineNumber() {
int caretOffset = editor.getCaretModel().getOffset();

return document.getLineNumber(caretOffset);
}

/**
* @return String Selected text
*/
private String getSelectedText() {
CharSequence editorText = document.getCharsSequence();

return UtilsTextual.getSubString(editorText, offsetSelectionStart, offsetSelectionEnd);
}

/**
* Setup and display wrap options dialog
*
Expand All @@ -83,7 +101,7 @@ public DialogWrapOptions showWrapOptions(Boolean isMultiLineSelection) {
optionsDialog.setTextFieldPrefix(Preferences.getWrapPrefix());
optionsDialog.setTextFieldPostfix(Preferences.getWrapPostfix());

if( Preferences.getMultiLineWrapMode().equals(DialogWrapOptions.MODE_WRAP_WHOLE)) {
if( Preferences.getMultiLineWrapMode().equals(DialogWrapOptions.MODE_WRAP_WHOLE) ) {
optionsDialog.wholeSelectionRadioButton.setSelected(true);
} else {
optionsDialog.eachLineRadioButton.setSelected(true);
Expand Down Expand Up @@ -178,9 +196,11 @@ private void wrapMultiLineSelection(String prefix, String postfix) {
lineText = lineText.replaceAll("\n", "");
document.replaceString(offsetLineStart + prefixLen, offsetLineEnd + prefixLen, lineText);
}

// Update selection: all lines of selection fully
selectionModel.setSelection(document.getLineStartOffset(lineNumberSelectionStart), document.getLineEndOffset(lineNumberSelectionEnd));
selectionModel.setSelection(
document.getLineStartOffset(lineNumberSelectionStart),
document.getLineEndOffset(lineNumberSelectionEnd)
);
}

/**
Expand All @@ -190,10 +210,7 @@ private void wrapMultiLineSelection(String prefix, String postfix) {
* @param postfix
*/
private void wrapSingleLinedSelection(String prefix, String postfix) {
CharSequence editorText = document.getCharsSequence();
String selectedText = UtilsTextual.getSubString(editorText, offsetSelectionStart, offsetSelectionEnd);

String wrappedString = prefix + selectedText + postfix;
String wrappedString = prefix + getSelectedText() + postfix;
document.replaceString(offsetSelectionStart, offsetSelectionEnd, wrappedString);

// Update selection
Expand All @@ -207,12 +224,10 @@ private void wrapSingleLinedSelection(String prefix, String postfix) {
* @param postfix
*/
private void wrapCaretLine(String prefix, String postfix) {
int caretOffset = editor.getCaretModel().getOffset();
int lineNumber = document.getLineNumber(caretOffset);

int lineNumber = getCaretLineNumber();
int offsetLineStart = document.getLineStartOffset(lineNumber);
String lineText = UtilsTextual.extractLine(document, lineNumber);
int offsetLineEnd = offsetLineStart + lineText.length() - 1;
String lineText = UtilsTextual.extractLine(document, lineNumber);
int offsetLineEnd = offsetLineStart + lineText.length() - 1;

document.insertString(offsetLineEnd, postfix);
document.insertString(offsetLineStart, prefix);
Expand All @@ -233,15 +248,13 @@ private void wrapCaretLine(String prefix, String postfix) {
* @param postfix
*/
private void unwrapCaretLine(String prefix, String postfix) {
int caretOffset = editor.getCaretModel().getOffset();
int lineNumber = document.getLineNumber(caretOffset);

int lineNumber = getCaretLineNumber();
int offsetLineStart = document.getLineStartOffset(lineNumber);
String lineText = UtilsTextual.extractLine(document, lineNumber);
int offsetLineEnd = offsetLineStart + lineText.length() - 1;
String lineText = UtilsTextual.extractLine(document, lineNumber);
int offsetLineEnd = offsetLineStart + lineText.length() - 1;

this.offsetSelectionStart = offsetLineStart;
this.offsetSelectionEnd = offsetLineEnd;
this.offsetSelectionStart = offsetLineStart;
this.offsetSelectionEnd = offsetLineEnd;

this.unwrapSingleLinedSelection(prefix, postfix);
}
Expand All @@ -254,7 +267,7 @@ private void unwrapCaretLine(String prefix, String postfix) {
*/
private void unwrapSingleLinedSelection(String prefix, String postfix) {
CharSequence editorText = document.getCharsSequence();
String unwrappedString = UtilsTextual.getSubString(editorText, offsetSelectionStart, offsetSelectionEnd);
String unwrappedString = UtilsTextual.getSubString(editorText, offsetSelectionStart, offsetSelectionEnd);

unwrappedString = UtilsTextual.unwrap(unwrappedString, prefix, postfix);

Expand Down Expand Up @@ -285,7 +298,31 @@ private void unwrapMultiLineSelection(String prefix, String postfix) {
}

// Update selection: all lines of selection fully
selectionModel.setSelection(document.getLineStartOffset(lineNumberSelectionStart), document.getLineEndOffset(lineNumberSelectionEnd));
selectionModel.setSelection(
document.getLineStartOffset(lineNumberSelectionStart),
document.getLineEndOffset(lineNumberSelectionEnd)
);
}

/**
* @param prefix
* @param postfix
* @return boolean Is caret line or selection wrapped into given pre/postfix?
*/
public boolean isWrapped(String prefix, String postfix) {
String text = hasSelection
? document.getText().substring(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd() )
: UtilsTextual.extractLine(document, getCaretLineNumber() );
text = text.trim();

if( ! Preferences.getMultiLineWrapMode().equals(DialogWrapOptions.MODE_WRAP_WHOLE ) ) {
if( text.contains("\n") ) {
// Wrap mode works on every line: Analyze first line only
text = text.split("\n")[0].trim();
}
}

return text.startsWith(prefix) && text.endsWith(postfix);
}

}
Expand Up @@ -150,7 +150,7 @@
<children>
<component id="6e36c" class="javax.swing.JRadioButton" binding="quickWrapRadioButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
Expand All @@ -159,18 +159,18 @@
</component>
<component id="91752" class="javax.swing.JRadioButton" binding="quickUnwrapRadioButton" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Unw&amp;rap"/>
</properties>
</component>
<component id="be1b8" class="javax.swing.JRadioButton" binding="quickAutodetectRadioButton">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Autodetect"/>
<text value="Auto&amp;detect"/>
</properties>
</component>
</children>
Expand Down
32 changes: 19 additions & 13 deletions src/com/kstenschke/realigner/resources/forms/DialogWrapOptions.java
Expand Up @@ -58,7 +58,7 @@ public class DialogWrapOptions extends JDialog {
public static final int MODE_WRAP_WHOLE = 1;

// Operations
private static final int OPERATION_CANCEL = 0;
public static final int OPERATION_CANCEL = 0;
public static final int OPERATION_WRAP = 1;
public static final int OPERATION_UNWRAP = 2;
public static final int OPERATION_AUTODETECT = 3;
Expand Down Expand Up @@ -86,7 +86,7 @@ public DialogWrapOptions(Boolean isMultiLineSelection) {
buttonSave.addActionListener(this.getActionListenerSaveQuickWrapButton());
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
onOk();
}
});
buttonUnwrap.addActionListener(new ActionListener() {
Expand All @@ -101,8 +101,8 @@ public void actionPerformed(ActionEvent e) {
});

// Add listeners to radio buttons
eachLineRadioButton.addActionListener(this.getActionListenerMulitLineModeRadio(MODE_WRAP_EACH_LINE));
wholeSelectionRadioButton.addActionListener(this.getActionListenerMulitLineModeRadio(MODE_WRAP_WHOLE));
eachLineRadioButton.addActionListener(this.getActionListenerMultiLineModeRadio(MODE_WRAP_EACH_LINE));
wholeSelectionRadioButton.addActionListener(this.getActionListenerMultiLineModeRadio(MODE_WRAP_WHOLE));
quickWrapRadioButton.addActionListener(this.getActionListenerQuickModeRadio(OPERATION_WRAP));
quickUnwrapRadioButton.addActionListener( this.getActionListenerQuickModeRadio(OPERATION_UNWRAP) );
quickAutodetectRadioButton.addActionListener( this.getActionListenerQuickModeRadio(OPERATION_AUTODETECT) );
Expand Down Expand Up @@ -180,7 +180,7 @@ public void actionPerformed(ActionEvent e) {
* @param mode Un/wrap
* @return ActionListener
*/
private ActionListener getActionListenerMulitLineModeRadio(final Integer mode) {
private ActionListener getActionListenerMultiLineModeRadio(final Integer mode) {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Expand Down Expand Up @@ -214,7 +214,6 @@ private void addQuickWrapButtons(Object[] allButtonsLabels, Object[] allButtonPr
panelWrapButtonsContainer.add(wrapButton, new GridConstraints(i, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));

// Add button action
final DialogWrapOptions dialog = this;
final String prefix = allButtonPrefixConfigs[i].toString();
final String postfix = allButtonPostfixConfigs[i].toString();

Expand All @@ -225,11 +224,14 @@ public void actionPerformed(ActionEvent e) {
setTextFieldPrefix(prefix);
setTextFieldPostfix(postfix);

if( dialog.quickWrapRadioButton.isSelected() ) {
onOK();
} else {
onUnwrap();
}
int operation = quickAutodetectRadioButton.isSelected()
? OPERATION_AUTODETECT
: (quickWrapRadioButton.isSelected()
? OPERATION_WRAP
: OPERATION_UNWRAP
);

onOK(operation);
}
});

Expand All @@ -242,8 +244,12 @@ public void actionPerformed(ActionEvent e) {
/**
* Handle click ok event
*/
private void onOK() {
clickedOperation = OPERATION_WRAP;
void onOk() {
this.onOK(OPERATION_WRAP);
}

private void onOK(int clickedOperation) {
this.clickedOperation = clickedOperation;
dispose();
}

Expand Down

0 comments on commit 6e224bf

Please sign in to comment.