diff --git a/build.gradle b/build.gradle index ef49795..e8d78de 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.2.21' + ext.kotlin_version = '1.2.30' repositories { mavenCentral() maven { url "https://jitpack.io" } @@ -26,12 +26,8 @@ allprojects { } } -def pluginVersion = '0.2.1' - group 'com.github.zxj5470' -version pluginVersion - - +version plugin_version repositories { mavenCentral() diff --git a/src/main/kotlin/cn/wjdghd/MainComponent.kt b/src/main/kotlin/cn/wjdghd/MainComponent.kt index 980184e..6409bf6 100644 --- a/src/main/kotlin/cn/wjdghd/MainComponent.kt +++ b/src/main/kotlin/cn/wjdghd/MainComponent.kt @@ -1,8 +1,8 @@ package cn.wjdghd -import cn.wjdghd.constants.RuntimeConstants.* import cn.wjdghd.entity.beginSpaces import cn.wjdghd.entity.splitWithParams +import com.github.zxj5470.bugktdoc.constants.* import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.PlatformDataKeys @@ -37,7 +37,7 @@ class MainComponent : ApplicationComponent { // thisLine : get /** with spaces val thisLine = getThisLine(editor) - val realNextLine = getRealNextLine(editor) + val realNextLine = getFunctionNextLine(editor) val realNext = getRealNext(editor) //avoid to mul-replaced @@ -85,20 +85,20 @@ fun stringFactory(thisLine: String, realNextLine: String, realNext: String): Str val sb = StringBuilder() sb.append(beginBeforeEachLine) sb.append(thisLine.trim()) - sb.append(LF) + sb.append(DocControl.LF) // ` * ` in each line stringLines.forEach { sb.append(beginBeforeEachLine) - sb.append(DOC_INNER) + sb.append(DocControl.INNER) if (it.isNotEmpty()) { - sb.append(PARAM) + sb.append(DocDecoration.PARAM) sb.append(it) - sb.append(LINE_SPLIT_COLON) + sb.append(DocControl.TYPE_SPLIT_COLON) } - sb.append(LF) + sb.append(DocControl.LF) } sb.append(beginBeforeEachLine) - sb.append(DOC_END) + sb.append(DocControl.END) return realNext.replace(thisLine, sb.toString()) } @@ -176,7 +176,7 @@ fun getRealNext(editor: Editor): String { return before + functionHead } -fun getRealNextLine(editor: Editor): String { +fun getFunctionNextLine(editor: Editor): String { val document = editor.document val caretModel = editor.caretModel val caretOffset = caretModel.offset diff --git a/src/main/kotlin/cn/wjdghd/constants/RuntimeConstants.java b/src/main/kotlin/cn/wjdghd/constants/RuntimeConstants.java deleted file mode 100644 index ee7b632..0000000 --- a/src/main/kotlin/cn/wjdghd/constants/RuntimeConstants.java +++ /dev/null @@ -1,14 +0,0 @@ -package cn.wjdghd.constants; - -/** - * Created by Administrator on 2017/7/30 0030. - */ -public class RuntimeConstants { - public static final String LF ="\n"; - public static final String PARAM="@param"; - public static final String SPACE=" "; - public static final String RETURN="@return"; - public static final String DOC_INNER=" * "; - public static final String LINE_SPLIT_COLON=" : "; - public static final String DOC_END=" */"; -} diff --git a/src/main/kotlin/cn/wjdghd/entity/StringExtensions.kt b/src/main/kotlin/cn/wjdghd/entity/StringExtensions.kt index 0d0e942..e09fb2e 100644 --- a/src/main/kotlin/cn/wjdghd/entity/StringExtensions.kt +++ b/src/main/kotlin/cn/wjdghd/entity/StringExtensions.kt @@ -4,17 +4,11 @@ import com.intellij.util.containers.Stack import java.util.* fun String.beginSpaces(): String { - val sb=StringBuilder() - println(this) - for(it in this){ - if(it in " \t"){ - sb.append(it) + return buildString { + this@beginSpaces.filter { it in " \t" }.forEach { + append(it) } - else break } - println("str:"+sb.toString()) - println(sb.toString().length) - return sb.toString() } fun String.splitWithParams(): LinkedList { diff --git a/src/main/kotlin/com/github/zxj5470/Enter.kt b/src/main/kotlin/com/github/zxj5470/Enter.kt deleted file mode 100644 index 7a44314..0000000 --- a/src/main/kotlin/com/github/zxj5470/Enter.kt +++ /dev/null @@ -1,89 +0,0 @@ -package com.github.zxj5470 - -import cn.wjdghd.* -import cn.wjdghd.constants.RuntimeConstants.* -import cn.wjdghd.entity.beginSpaces -import cn.wjdghd.entity.splitWithParams -import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate -import com.intellij.openapi.actionSystem.DataContext -import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.command.CommandProcessor -import com.intellij.openapi.editor.Editor -import com.intellij.openapi.editor.actionSystem.EditorActionHandler -import com.intellij.openapi.util.Ref -import com.intellij.openapi.util.TextRange -import com.intellij.psi.PsiFile - -class BKDTypedHandler : EnterHandlerDelegate { - var ok = false - - override fun postProcessEnter(psiFile: PsiFile, editor: Editor, context: DataContext): EnterHandlerDelegate.Result { - return if (ok) EnterHandlerDelegate.Result.Stop else EnterHandlerDelegate.Result.Default - } - - - override fun preprocessEnter(p0: PsiFile, editor: Editor, p2: Ref, p3: Ref, p4: DataContext, p5: EditorActionHandler?): EnterHandlerDelegate.Result { - ok = getCurrentLine(editor).endsWith("/**") - if (ok) { - val document = editor.document - val thisLine = getCurrentLine(editor) - val realNextLine = getRealNextLine(editor) - // avoid `*` multi-match - if(realNextLine.trim().startsWith("*")){ - return EnterHandlerDelegate.Result.Default - } - val realNext = getRealNext(editor) - val stringFac = stringFactory(thisLine, realNextLine, realNext) -// println("---thisLine---\n" + -// "$thisLine \n" + -// "---realNextLine---\n" + -// "$realNextLine \n" + -// "---$realNext---\n" + -// "$stringFac\n" + -// "---END---") - val replaceString = document.text.replace(realNext, stringFac) - ApplicationManager.getApplication().runWriteAction { - CommandProcessor.getInstance().runUndoTransparentAction { - document.setText(replaceString) - } - } - println(replaceString) - } - return if (ok) EnterHandlerDelegate.Result.Stop else EnterHandlerDelegate.Result.Default - } -} - -fun stringFactory(thisLine: String, realNextLine: String, realNext: String): String { - val beginIndent = realNextLine.beginSpaces() - val docLines = getFunctionDeclarationLine(realNextLine).splitWithParams() - //` /** ` in first line - val sb = StringBuilder() - sb.append(thisLine) - sb.append(LF) - // ` * ` in each line - docLines.forEach { - sb.append(beginIndent) - sb.append(DOC_INNER) - if (it.isNotEmpty()) { - sb.append(PARAM) - sb.append(SPACE) - sb.append(it) - sb.append(LINE_SPLIT_COLON) - } - sb.append(LF) - } - sb.append(beginIndent) - sb.append(DOC_END) - return realNext.replace(thisLine, sb.toString()) -} - - -fun getCurrentLine(editor: Editor): String { - val document = editor.document - val caretModel = editor.caretModel - val caretOffset = caretModel.offset - val lineNum = document.getLineNumber(caretOffset) - val lineStartOffset = document.getLineStartOffset(lineNum) - val lineEndOffset = document.getLineEndOffset(lineNum) - return document.getText(TextRange(lineStartOffset, lineEndOffset)) -} \ No newline at end of file diff --git a/src/main/kotlin/com/github/zxj5470/bugktdoc/Enter.kt b/src/main/kotlin/com/github/zxj5470/bugktdoc/Enter.kt new file mode 100644 index 0000000..7bae06b --- /dev/null +++ b/src/main/kotlin/com/github/zxj5470/bugktdoc/Enter.kt @@ -0,0 +1,35 @@ +package com.github.zxj5470.bugktdoc + +import cn.wjdghd.* +import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.command.CommandProcessor +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.actionSystem.EditorActionHandler +import com.intellij.openapi.util.Ref +import com.intellij.psi.PsiFile + +class BugKotlinEnterHandlerDelegate : EnterHandlerDelegate { + var ok = false + + override fun postProcessEnter(psiFile: PsiFile, editor: Editor, context: DataContext): EnterHandlerDelegate.Result { + if (ok) { + val document = editor.document + val offset = editor.caretModel.currentCaret.offset + val stringFac = genDocString(getFunctionNextLine(editor)) + ApplicationManager.getApplication().runWriteAction { + CommandProcessor.getInstance().runUndoTransparentAction { + document.insertString(offset, stringFac) + } + } + } + return EnterHandlerDelegate.Result.Continue + } + + + override fun preprocessEnter(p0: PsiFile, editor: Editor, p2: Ref, p3: Ref, p4: DataContext, p5: EditorActionHandler?): EnterHandlerDelegate.Result { + ok = getCurrentLine(editor).endsWith("/**") && !editorNextLine(editor).trim().startsWith("*") + return EnterHandlerDelegate.Result.Continue + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/zxj5470/bugktdoc/constants/constant.kt b/src/main/kotlin/com/github/zxj5470/bugktdoc/constants/constant.kt new file mode 100644 index 0000000..7f95517 --- /dev/null +++ b/src/main/kotlin/com/github/zxj5470/bugktdoc/constants/constant.kt @@ -0,0 +1,20 @@ +package com.github.zxj5470.bugktdoc.constants + +/** + * @author: zxj5470 + * @date: 2018/4/1 + */ +object DocControl { + val END = " */" + val INNER = " * " + val LF = "\n" + val SPACE = " " + val TYPE_SPLIT_COLON = " : " +} + +object DocDecoration { + val AUTHOR = "@author" + val PARAM = "@param" + val RETURN = "@return" + val THROWS = "@throws" +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/zxj5470/bugktdoc/utils.kt b/src/main/kotlin/com/github/zxj5470/bugktdoc/utils.kt new file mode 100644 index 0000000..1d6496c --- /dev/null +++ b/src/main/kotlin/com/github/zxj5470/bugktdoc/utils.kt @@ -0,0 +1,48 @@ +package com.github.zxj5470.bugktdoc + +import cn.wjdghd.entity.splitWithParams +import cn.wjdghd.getFunctionDeclarationLine +import com.github.zxj5470.bugktdoc.constants.* +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange + +/** + * @author: zxj5470 + * @date: 2018/4/1 + */ + +fun editorNextLine(editor: Editor): String { + val document = editor.document + val caretModel = editor.caretModel + val caretOffset = caretModel.offset + val lineNum = document.getLineNumber(caretOffset) + 1 + val lineStartOffset = document.getLineStartOffset(lineNum) + val lineEndOffset = document.getLineEndOffset(lineNum) + return document.getText(TextRange(lineStartOffset, lineEndOffset)) +} + +fun getCurrentLine(editor: Editor): String { + val document = editor.document + val caretModel = editor.caretModel + val caretOffset = caretModel.offset + val lineNum = document.getLineNumber(caretOffset) + val lineStartOffset = document.getLineStartOffset(lineNum) + val lineEndOffset = document.getLineEndOffset(lineNum) + return document.getText(TextRange(lineStartOffset, lineEndOffset)) +} + +fun genDocString(realNextLine: String): String = buildString { + append(DocControl.LF) + getFunctionDeclarationLine(realNextLine) + .splitWithParams() + .filter { it.isNotEmpty() } + .forEachIndexed { index, it -> + if (index >= 1) { + append(DocControl.LF) + } + append(DocControl.INNER) + append(DocDecoration.PARAM) + append(DocControl.SPACE) + append(it) + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 0240198..4d1491a 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -1,5 +1,5 @@ - cn.wjdghd.plugin.id + com.github.zxj5470.bugktdoc BugKotlinDocument 0.2.0 zxj5470 @@ -14,7 +14,7 @@ - +