Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions src/osmedile/intellij/stringmanip/SwitchFilePathSeparators.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,62 @@

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NotNull;

public class SwitchFilePathSeparators extends AbstractStringManipAction {

@Override
public String transformSelection(Editor editor, DataContext dataContext, String s, Object additionalParam) {
String s1;
if (s.contains("/")) {
s1 = s.replace("/", "\\");
} else {
s1 = s.replace("\\", "/");
}
return s1;
}
protected SwitchFilePathSeparators() {
}

protected SwitchFilePathSeparators(boolean setupHandler) {
super(setupHandler);
}


enum SwitchTo {
op_Unknown,
op_SlashToBackslash,
op_BackslashToSlash
}

private SwitchTo replaceOperation = SwitchTo.op_Unknown;

@NotNull
@Override
public Pair beforeWriteAction(Editor editor, DataContext dataContext) {
replaceOperation = SwitchTo.op_Unknown;
return super.beforeWriteAction(editor, dataContext);
}

@Override
public String transformByLine(String s) {
throw new UnsupportedOperationException();
String s1;
if (replaceOperation == SwitchTo.op_Unknown) {
int fslash = s.indexOf('/');
int bslash = s.indexOf('\\');

boolean slashDetected = (bslash >= 0) || (fslash >= 0);
if (slashDetected) {
if ((fslash >= 0) && (bslash >= 0)) {
replaceOperation = (bslash < fslash) ? SwitchTo.op_BackslashToSlash : SwitchTo.op_SlashToBackslash;
} else {
replaceOperation = (bslash >= 0) ? SwitchTo.op_BackslashToSlash : SwitchTo.op_SlashToBackslash;
}
}
}
switch (replaceOperation) {
case op_SlashToBackslash:
s1 = s.replace("/", "\\");
break;
case op_BackslashToSlash:
s1 = s.replace("\\", "/");
break;
case op_Unknown:
default:
s1 = s;
break;
}
return s1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package osmedile.intellij.stringmanip;

import org.junit.Assert;
import org.junit.Test;

public class SwitchFilePathSeparatorsTest {

protected SwitchFilePathSeparators action;

@Test
public void testSwitchFilePathSeparators_BackslashToSlash() {
action = new SwitchFilePathSeparators(false);
Assert.assertEquals("Foo bar Wee All", action.transformByLine("Foo bar Wee All"));
// first found determine op
Assert.assertEquals("Foo/bar/foo", action.transformByLine("Foo\\bar\\foo"));
// second works same
Assert.assertEquals("Foo/bar/foo", action.transformByLine("Foo\\bar\\foo"));
// don't change action
Assert.assertEquals("Foo/bar/foo", action.transformByLine("Foo/bar/foo"));
}

@Test
public void testSwitchFilePathSeparators_BackslashToSlashMix() {
action = new SwitchFilePathSeparators(false);
Assert.assertEquals("Foo bar Wee All", action.transformByLine("Foo bar Wee All"));
// first found determine op
Assert.assertEquals("Foo/bar/foo/bar", action.transformByLine("Foo\\bar\\foo/bar"));
// second works same
Assert.assertEquals("Foo/bar/foo/bar", action.transformByLine("Foo\\bar\\foo/bar"));
// don't change action
Assert.assertEquals("Foo/bar/foo", action.transformByLine("Foo/bar/foo"));
}

@Test
public void testSwitchFilePathSeparators_BackslashToSlashSingle() {
action = new SwitchFilePathSeparators(false);
Assert.assertEquals("Foo/bar/foo/bar", action.transformByLine("Foo\\bar\\foo\\bar"));
}


@Test
public void testSwitchFilePathSeparators_SlashToBackSlash() {
action = new SwitchFilePathSeparators(false);
// first found determine op
Assert.assertEquals("Foo\\bar\\foo", action.transformByLine("Foo/bar/foo"));
// second works same
Assert.assertEquals("Foo\\bar\\foo", action.transformByLine("Foo/bar/foo"));
// don't change action
Assert.assertEquals("Foo\\bar\\foo", action.transformByLine("Foo\\bar\\foo"));
}
@Test
public void testSwitchFilePathSeparators_SlashToBackSlashMix() {
action = new SwitchFilePathSeparators(false);
Assert.assertEquals("Foo bar Wee All", action.transformByLine("Foo bar Wee All"));
// first found determine op
Assert.assertEquals("Foo\\bar\\foo\\bar", action.transformByLine("Foo/bar/foo\\bar"));
// second works same
Assert.assertEquals("Foo\\bar\\foo\\bar", action.transformByLine("Foo/bar/foo\\bar"));
// don't change action
Assert.assertEquals("Foo\\bar\\foo", action.transformByLine("Foo\\bar\\foo"));
}

@Test
public void testSwitchFilePathSeparators_SlashToBackSlashSingle() {
action = new SwitchFilePathSeparators(false);
Assert.assertEquals("Foo\\bar\\foo", action.transformByLine("Foo/bar/foo"));
}

}