Skip to content

Commit

Permalink
[v0.2.?] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zxj5470 committed Apr 1, 2018
1 parent edfedff commit 14e0d15
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 129 deletions.
8 changes: 2 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.21'
ext.kotlin_version = '1.2.30'
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
Expand All @@ -26,12 +26,8 @@ allprojects {
}
}

def pluginVersion = '0.2.1'

group 'com.github.zxj5470'
version pluginVersion


version plugin_version

repositories {
mavenCentral()
Expand Down
18 changes: 9 additions & 9 deletions src/main/kotlin/cn/wjdghd/MainComponent.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions src/main/kotlin/cn/wjdghd/constants/RuntimeConstants.java

This file was deleted.

12 changes: 3 additions & 9 deletions src/main/kotlin/cn/wjdghd/entity/StringExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
Expand Down
89 changes: 0 additions & 89 deletions src/main/kotlin/com/github/zxj5470/Enter.kt

This file was deleted.

35 changes: 35 additions & 0 deletions src/main/kotlin/com/github/zxj5470/bugktdoc/Enter.kt
Original file line number Diff line number Diff line change
@@ -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<Int>, p3: Ref<Int>, p4: DataContext, p5: EditorActionHandler?): EnterHandlerDelegate.Result {
ok = getCurrentLine(editor).endsWith("/**") && !editorNextLine(editor).trim().startsWith("*")
return EnterHandlerDelegate.Result.Continue
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/com/github/zxj5470/bugktdoc/constants/constant.kt
Original file line number Diff line number Diff line change
@@ -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"
}
48 changes: 48 additions & 0 deletions src/main/kotlin/com/github/zxj5470/bugktdoc/utils.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<idea-plugin>
<id>cn.wjdghd.plugin.id</id>
<id>com.github.zxj5470.bugktdoc</id>
<name>BugKotlinDocument</name>
<version>0.2.0</version>
<vendor email="[email protected]" url="https://github.com/zxj5470/BugKotlinDocument/issues">zxj5470</vendor>
Expand All @@ -14,7 +14,7 @@
<idea-version since-build="139"/>

<extensions defaultExtensionNs="com.intellij">
<enterHandlerDelegate implementation="com.github.zxj5470.BKDTypedHandler"/>
<enterHandlerDelegate implementation="com.github.zxj5470.bugktdoc.BugKotlinEnterHandlerDelegate"/>
</extensions>

<actions>
Expand Down

0 comments on commit 14e0d15

Please sign in to comment.