Skip to content

Commit

Permalink
[v0.2.?] add kotlin grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
zxj5470 committed Apr 1, 2018
1 parent 14e0d15 commit 7edabea
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[*]
charset=utf-8
end_of_line=lf
indent_style=tab
tab_width=3
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ repositories {

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}

Expand Down
119 changes: 119 additions & 0 deletions kotlinFunction.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
KotlinFunctionSignature::=
modifiers
"fun"
typeParameters? // generics
(type ".")?
SimpleName
typeParameters?
valueParameters (":" type)?
typeConstraints

//TODO
//TODO
//TODO

// You know it.
private SimpleName ::= <java identifier>
"`" <java identifier> "`"

//TODO BEGIN modifiers
modifiers::=(modifier | annotations)*
private modifier::= classModifier
| accessModifier
| varianceAnnotation
| memberModifier
| parameterModifier
| typeParameterModifier
| functionModifier
| propertyModifier

private classModifier ::= "abstract"
| "final"
| "enum"
| "open"
| "annotation"
| "sealed"
| "data"

private memberModifier::="override"
| "open"
| "final"
| "abstract"
| "lateinit"

private accessModifier::="private"
| "protected"
| "public"
| "internal"

private varianceAnnotation::="in"
| "out"

private parameterModifier::="noinline"
| "crossinline"
| "vararg"

private typeParameterModifier::="reified"

private functionModifier::="tailrec"
| "operator"
| "infix"
| "inline"
| "external"
| suspendModifier
private propertyModifier::="const"
private suspendModifier::="suspend"

private annotations ::= (annotation | annotationList)*
private annotationList ::= "@" (annotationUseSiteTarget ":")? unescapedAnnotation
private annotationUseSiteTarget ::= "field"
| "file"
| "property"
| "get"
| "set"
| "receiver"
| "param"
| "setparam"
| "delegate"
private unescapedAnnotation ::= SimpleName {"."} typeArguments? valueArguments?
private typeArguments ::= "<" type {","} ">"
private valueArguments ::= "(" (SimpleName "=")? "*"? expression {","} ")"


private annotation ::= "@" (annotationUseSiteTarget ":")? "[" unescapedAnnotation+ "]"

//TODO END modifiers

// TODO BEGIN typeParameters
typeParameters ::="<" typeParameter (comma typeParameter)* ">"
private comma::=","
private typeParameter::= modifiers SimpleName (":" userType)?
// TODO END typeParameters

// TODO BEGIN type
type::=typeModifiers typeReference
private typeModifiers ::= (suspendModifier | annotations)*
private typeReference ::= "(" typeReference ")"
| functionType
| userType
// | nullableType
| "dynamic"

private functionType ::= (type ".")? "(" parameter{","}? ")" "->" type?
private parameter::= SimpleName ":" type

private userType::= simpleUserType{"."}
private simpleUserType::= SimpleName ("<" (optionalProjection type | "*"){","} ">")?
private optionalProjection ::= varianceAnnotation

//private nullableType ::= typeReference "?"

// TODO END type

valueParameters ::= "(" functionParameter{","}? ")"
private functionParameter ::= modifiers ("val" | "var")? parameter ("=" expression)?

typeConstraints ::= ("where" typeConstraint{","})?
private typeConstraint ::= annotations SimpleName ":" type


38 changes: 1 addition & 37 deletions src/main/kotlin/cn/wjdghd/MainComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cn.wjdghd
import cn.wjdghd.entity.beginSpaces
import cn.wjdghd.entity.splitWithParams
import com.github.zxj5470.bugktdoc.constants.*
import com.github.zxj5470.bugktdoc.getFunctionNextLine

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
Expand Down Expand Up @@ -175,40 +176,3 @@ fun getRealNext(editor: Editor): String {
val before = sub.substring(0, index)
return before + functionHead
}

fun getFunctionNextLine(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 s = document.text.substring(lineStartOffset)
val charStack = Stack<Char>()
var top: Char
var indexEnd: Int = 0
for (i in s.indices) {
top = if (charStack.empty()) ' ' else charStack.peek()
when (s[i]) {
'\'' -> {
if (top != '\'') charStack.push(s[i])
else charStack.pop()
}
'\"' -> {
if (top != '\"') charStack.push(s[i])
else charStack.pop()
}
'(', '{', '<' -> charStack.push(s[i])
')' -> if (top == '(') charStack.pop()
'}' -> if (top == '{') charStack.pop()
'>' -> if (top == '<') charStack.pop()
}
if (charStack.isEmpty()) {
if (s[i] == '}') {
indexEnd = i + 1
break
}
}
}
val functionHead = s.substring(1, indexEnd)
return functionHead
}
15 changes: 9 additions & 6 deletions src/main/kotlin/com/github/zxj5470/bugktdoc/Enter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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
Expand All @@ -11,10 +10,15 @@ import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiFile

class BugKotlinEnterHandlerDelegate : EnterHandlerDelegate {
var ok = false

var canGenerateDocument = false

fun asda(): String??????????? {
return ""
}

override fun postProcessEnter(psiFile: PsiFile, editor: Editor, context: DataContext): EnterHandlerDelegate.Result {
if (ok) {
if (canGenerateDocument) {
val document = editor.document
val offset = editor.caretModel.currentCaret.offset
val stringFac = genDocString(getFunctionNextLine(editor))
Expand All @@ -27,9 +31,8 @@ class BugKotlinEnterHandlerDelegate : EnterHandlerDelegate {
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("*")
override fun preprocessEnter(file: PsiFile, editor: Editor, caretOffset: Ref<Int>, caretAdvance: Ref<Int>, dataContext: DataContext, originalHandler: EditorActionHandler?): EnterHandlerDelegate.Result {
canGenerateDocument = getCurrentLine(editor).endsWith("/**") && !editorNextLine(editor).trim().startsWith("*")
return EnterHandlerDelegate.Result.Continue
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/com/github/zxj5470/bugktdoc/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cn.wjdghd.getFunctionDeclarationLine
import com.github.zxj5470.bugktdoc.constants.*
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.util.containers.Stack

/**
* @author: zxj5470
Expand Down Expand Up @@ -45,4 +46,41 @@ fun genDocString(realNextLine: String): String = buildString {
append(DocControl.SPACE)
append(it)
}
}

fun getFunctionNextLine(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 s = document.text.substring(lineStartOffset)
val charStack = Stack<Char>()
var top: Char
var indexEnd: Int = 0
for (i in s.indices) {
top = if (charStack.empty()) ' ' else charStack.peek()
when (s[i]) {
'\'' -> {
if (top != '\'') charStack.push(s[i])
else charStack.pop()
}
'\"' -> {
if (top != '\"') charStack.push(s[i])
else charStack.pop()
}
'(', '{', '<' -> charStack.push(s[i])
')' -> if (top == '(') charStack.pop()
'}' -> if (top == '{') charStack.pop()
'>' -> if (top == '<') charStack.pop()
}
if (charStack.isEmpty()) {
if (s[i] == '}') {
indexEnd = i + 1
break
}
}
}
val functionHead = s.substring(1, indexEnd)
return functionHead
}

0 comments on commit 7edabea

Please sign in to comment.