|
3 | 3 | import org.eclipse.core.commands.AbstractHandler; |
4 | 4 | import org.eclipse.core.commands.ExecutionEvent; |
5 | 5 | 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; |
13 | 7 | import org.eclipse.jface.text.IDocument; |
14 | 8 | import org.eclipse.jface.text.IRegion; |
15 | 9 | import org.eclipse.jface.text.ITextSelection; |
16 | 10 | import org.eclipse.jface.text.TextSelection; |
17 | 11 | 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 | + |
18 | 17 | import com.sun.xml.internal.ws.util.StringUtils; |
19 | 18 |
|
20 | 19 | public class Convertor extends AbstractHandler { |
21 | 20 |
|
22 | 21 | 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 | | - |
29 | 22 | 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 | + } |
44 | 32 |
|
45 | | - if (sel instanceof TextSelection) { |
46 | | - final TextSelection textSel = (TextSelection)sel; |
47 | | - String originalText = ((String) textSel.getText()).trim(); |
| 33 | + } catch(Exception e) {} |
48 | 34 |
|
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); |
54 | 55 |
|
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(); |
56 | 81 |
|
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(); |
61 | 85 | } |
| 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; |
62 | 119 | } |
63 | | - } catch (Exception ex) { |
64 | | - ex.printStackTrace(); |
65 | 120 | } |
66 | 121 | return null; |
67 | 122 | } |
| 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 | + } |
68 | 140 |
|
69 | | - public static String toCamelCase(final String init) { |
| 141 | + private String toCamelCase(final String init) { |
70 | 142 | final StringBuilder ret = new StringBuilder(init.length()); |
71 | | - |
72 | 143 | for (final String word : init.split(" ")) { |
73 | 144 | ret.append(StringUtils.capitalize(word)); |
74 | 145 | } |
75 | | - |
76 | 146 | return ret.toString(); |
77 | 147 | } |
78 | 148 | } |
0 commit comments