-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateNewPddAction.kt
43 lines (39 loc) · 1.87 KB
/
CreateNewPddAction.kt
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
package com.aivinog1.pdd.plugin.template
import com.intellij.lang.LanguageCommenters
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
/**
* This action creates a new PDD todo.
* @todo #38:30m Consider moving to the EditorAction. I think, this class would be more suitable for this task.
*/
class CreateNewPddAction(private val logger: Logger = Logger.getInstance("CreateNewPddAction")) : AnAction("Create a new PDD.") {
override fun actionPerformed(e: AnActionEvent) {
val file = e.getData(CommonDataKeys.PSI_FILE)
val editor = e.getData(CommonDataKeys.EDITOR)
val project = e.getData(CommonDataKeys.PROJECT)
if (file != null && editor != null && project != null) {
actionPerformed(file, editor, project)
}
}
private fun actionPerformed(file: PsiFile, editor: Editor, project: Project) {
val language = file.language
val commenter = LanguageCommenters.INSTANCE.forLanguage(language)
val lineCommentPrefix = commenter.lineCommentPrefix
if (editor.isInsertMode && lineCommentPrefix != null) {
val document = editor.document
if (document.isWritable) {
WriteCommandAction.runWriteCommandAction(project) {
// @todo #38:30 For now this action just comment line and nothing more. Needs to implement inserting a todo.
document.insertString(0, lineCommentPrefix)
}
}
logger.trace("Current language: $language. It's comment is $lineCommentPrefix.")
}
}
}