-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPythonTypeInfoAddedGroovyEvent.groovy
More file actions
57 lines (51 loc) · 1.96 KB
/
PythonTypeInfoAddedGroovyEvent.groovy
File metadata and controls
57 lines (51 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.CaretModel
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import me.aristotll.Provider
import static liveplugin.PluginUtil.*
/**
* Name: PythonTypeInfoAddedEvent<br>
* User: Yao<br>
* Date: 17/6/9<br>
* Time: 15:10<br>
*/
class PythonTypeInfoAddedGroovyEvent {
static final void eventClosure(AnActionEvent event) {
//Get all the required data from data keys
final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR)
final Project project = event.getRequiredData(CommonDataKeys.PROJECT)
/**
* http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/psi_files.html
*/
PsiFile psiFile = currentPsiFileIn(project)
final CaretModel cursor = editor.caretModel
PsiElement psiElement = psiFile.findElementAt(cursor.offset).parent
// inspect(psiElement)
if (psiElement == null) {
return
}
//Access document, caret, and selection
final Document document = editor.getDocument()
final int line = cursor.logicalPosition.line
//New instance of Runnable to make a replacement
Runnable runnable = new Runnable() {
@Override
void run() {
def type = Provider.getType(psiElement)
if (type == null) {
return
}
document.insertString(document.getLineEndOffset(line),
" # type: " + Provider.normalizeFunctionType(type))
}
}
//Making the replacement
WriteCommandAction.runWriteCommandAction(project, runnable)
}
}