Skip to content

Commit 0eaed6e

Browse files
committed
using editor settings to choose between tabs and spaces + refactored
1 parent 2c9fb19 commit 0eaed6e

3 files changed

Lines changed: 116 additions & 46 deletions

File tree

META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: TestNameGenerator
44
Bundle-SymbolicName: testNameGenerator;singleton:=true
5-
Bundle-Version: 1.0.2
5+
Bundle-Version: 1.0.3
66
Bundle-Activator: testnamegenerator.Activator
77
Require-Bundle: org.eclipse.ui,
88
org.eclipse.core.runtime,
5.1 KB
Binary file not shown.

src/testnamegenerator/handlers/Convertor.java

Lines changed: 115 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,146 @@
33
import org.eclipse.core.commands.AbstractHandler;
44
import org.eclipse.core.commands.ExecutionEvent;
55
import org.eclipse.core.commands.ExecutionException;
6-
import org.eclipse.ui.IEditorPart;
7-
import org.eclipse.ui.IWorkbench;
8-
import org.eclipse.ui.IWorkbenchPage;
9-
import org.eclipse.ui.IWorkbenchWindow;
10-
import org.eclipse.ui.PlatformUI;
11-
import org.eclipse.ui.texteditor.IDocumentProvider;
12-
import org.eclipse.ui.texteditor.ITextEditor;
6+
import org.eclipse.core.runtime.Platform;
137
import org.eclipse.jface.text.IDocument;
148
import org.eclipse.jface.text.IRegion;
159
import org.eclipse.jface.text.ITextSelection;
1610
import org.eclipse.jface.text.TextSelection;
1711
import org.eclipse.jface.viewers.ISelection;
12+
import org.eclipse.ui.IEditorPart;
13+
import org.eclipse.ui.IWorkbenchPage;
14+
import org.eclipse.ui.PlatformUI;
15+
import org.eclipse.ui.texteditor.ITextEditor;
16+
1817
import com.sun.xml.internal.ws.util.StringUtils;
1918

2019
public class Convertor extends AbstractHandler {
2120

2221
public Object execute(ExecutionEvent event) throws ExecutionException {
23-
IWorkbench wb = PlatformUI.getWorkbench();
24-
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
25-
IWorkbenchPage page = win.getActivePage();
26-
ISelection selection = page.getSelection();
27-
ITextSelection ts = (org.eclipse.jface.text.ITextSelection) selection;
28-
2922
try {
30-
IEditorPart part = page.getActiveEditor();
31-
32-
if (part instanceof ITextEditor) {
33-
final ITextEditor editor = (ITextEditor)part;
34-
IDocumentProvider prov = editor.getDocumentProvider();
35-
IDocument doc = prov.getDocument(editor.getEditorInput());
36-
37-
if (doc != null) {
38-
// mark and prepare
39-
int lineNumber = ts.getStartLine();
40-
IRegion lineInfo = doc.getLineInformation(lineNumber);
41-
42-
editor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
43-
ISelection sel = editor.getSelectionProvider().getSelection();
23+
IDocument doc = this.getDocument();
24+
ITextSelection ts = this.getSelection();
25+
int lineNumber = this.getCurrentLineNumber(ts);
26+
27+
if (lineNumber >= 0) {
28+
String originalText = this.getLineContents(lineNumber, doc);
29+
String preparedText = this.getPreparedTestMethod(originalText);
30+
this.generateTestMethod(preparedText, lineNumber, doc);
31+
}
4432

45-
if (sel instanceof TextSelection) {
46-
final TextSelection textSel = (TextSelection)sel;
47-
String originalText = ((String) textSel.getText()).trim();
33+
} catch(Exception e) {}
4834

49-
// replace
50-
String commentText = "\n\t/**\n\t * " + originalText.replace("*/", "* /") + "\n\t */\n\t";
51-
String methodNameText = originalText.replaceAll("\\P{Alnum}", " ").trim();
52-
methodNameText = (methodNameText.isEmpty()) ? "Blank" : toCamelCase(methodNameText);
53-
String methodText = "public function test" + methodNameText.replace(" ", "") + "() {\n\t\t$this->markTestIncomplete('implement me...');\n\t}\n\n";
35+
return null;
36+
}
37+
38+
private void generateTestMethod(String preparedText, int lineNumber, IDocument doc)
39+
{
40+
ITextEditor editor = this.getEditor();
41+
if (editor == null) {
42+
return;
43+
}
44+
try {
45+
IRegion lineInfo = doc.getLineInformation(lineNumber);
46+
editor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
47+
ISelection sel = editor.getSelectionProvider().getSelection();
48+
49+
if (sel instanceof TextSelection) {
50+
final TextSelection textSel = (TextSelection)sel;
51+
doc.replace(textSel.getOffset(), textSel.getLength(), preparedText);
52+
53+
// select temporary text
54+
lineInfo = doc.getLineInformation(lineNumber + 5);
5455

55-
doc.replace(textSel.getOffset(), textSel.getLength(), commentText + methodText);
56+
Boolean useSpaces = this.useSpacesForTabs();
57+
int offset = (useSpaces) ? 2 * this.getTabChar().length() : 2;
58+
editor.selectAndReveal(lineInfo.getOffset() + offset, lineInfo.getLength() - offset); // remove tabs from selection
59+
}
60+
} catch(Exception e) {}
61+
}
62+
63+
private String getPreparedTestMethod(String originalText)
64+
{
65+
String tab = this.getTabChar();
66+
String commentText = "\n" + tab + "/**\n" + tab + " * " + originalText.replace("*/", "* /") + "\n" + tab + " */\n" + tab + "";
67+
String methodNameText = originalText.replaceAll("\\P{Alnum}", " ").trim();
68+
methodNameText = (methodNameText.isEmpty()) ? "Blank" : this.toCamelCase(methodNameText);
69+
String methodText = "public function test" + methodNameText.replace(" ", "") + "() {\n" + tab + "" + tab + "$this->markTestIncomplete('implement me...');\n" + tab + "}\n";
70+
return commentText + methodText;
71+
}
72+
73+
private String getLineContents(int lineNumber, IDocument doc)
74+
{
75+
ITextEditor editor = this.getEditor();
76+
if (editor != null) {
77+
try {
78+
IRegion lineInfo = doc.getLineInformation(lineNumber);
79+
editor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
80+
ISelection sel = editor.getSelectionProvider().getSelection();
5681

57-
// select temporary text
58-
lineInfo = doc.getLineInformation(lineNumber + 5);
59-
editor.selectAndReveal(lineInfo.getOffset() + 2, lineInfo.getLength() - 2); // remove tabs from selection
60-
}
82+
if (sel instanceof TextSelection) {
83+
final TextSelection textSel = (TextSelection)sel;
84+
return ((String) textSel.getText()).trim();
6185
}
86+
} catch(Exception e) {}
87+
}
88+
return "";
89+
}
90+
91+
private ITextSelection getSelection()
92+
{
93+
return (org.eclipse.jface.text.ITextSelection) this.getPage().getSelection();
94+
}
95+
96+
private int getCurrentLineNumber(ITextSelection textSelection)
97+
{
98+
return textSelection.getStartLine();
99+
}
100+
101+
private ITextEditor getEditor()
102+
{
103+
try {
104+
IEditorPart part = this.getPage().getActiveEditor();
105+
if (part instanceof ITextEditor) {
106+
return (ITextEditor)part;
107+
}
108+
} catch (Exception ex) {}
109+
return null;
110+
}
111+
112+
private IDocument getDocument()
113+
{
114+
ITextEditor editor = this.getEditor();
115+
if (editor != null) {
116+
IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
117+
if (doc != null) {
118+
return doc;
62119
}
63-
} catch (Exception ex) {
64-
ex.printStackTrace();
65120
}
66121
return null;
67122
}
123+
124+
private IWorkbenchPage getPage()
125+
{
126+
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
127+
}
128+
129+
private Boolean useSpacesForTabs()
130+
{
131+
return Platform.getPreferencesService().getBoolean("org.eclipse.ui.editors", "spacesForTabs", false, null);
132+
}
133+
134+
private String getTabChar()
135+
{
136+
Boolean useSpaces = this.useSpacesForTabs();
137+
int tabWidth = Platform.getPreferencesService().getInt("org.eclipse.ui.editors", "tabWidth", 4, null);
138+
return (!useSpaces) ? "\t" : String.format("%"+tabWidth+"s", "");
139+
}
68140

69-
public static String toCamelCase(final String init) {
141+
private String toCamelCase(final String init) {
70142
final StringBuilder ret = new StringBuilder(init.length());
71-
72143
for (final String word : init.split(" ")) {
73144
ret.append(StringUtils.capitalize(word));
74145
}
75-
76146
return ret.toString();
77147
}
78148
}

0 commit comments

Comments
 (0)