-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(intellij): added initial Robot Framework file templates and bett…
…er syntax highlighting support based on a customized TextMate lexer/parser
- Loading branch information
Showing
43 changed files
with
1,051 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/BundledHelpers.kt
This file was deleted.
Oops, something went wrong.
112 changes: 112 additions & 0 deletions
112
intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/RobotCodeHelpers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package dev.robotcode.robotcode4ij | ||
|
||
import com.intellij.execution.configurations.GeneralCommandLine | ||
import com.intellij.execution.util.ExecUtil | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.application.PathManager | ||
import com.intellij.openapi.diagnostic.thisLogger | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.Key | ||
import com.jetbrains.python.sdk.pythonSdk | ||
import java.nio.file.Path | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.exists | ||
import kotlin.io.path.isRegularFile | ||
import kotlin.io.path.pathString | ||
|
||
class RobotCodeHelpers { | ||
companion object { | ||
val basePath: Path = PathManager.getPluginsDir().resolve("robotcode4ij").resolve("data") | ||
val bundledPath: Path = basePath.resolve("bundled") | ||
val toolPath: Path = bundledPath.resolve("tool") | ||
val robotCodePath: Path = toolPath.resolve("robotcode") | ||
val checkRobotVersion: Path = toolPath.resolve("utils").resolve("check_robot_version.py") | ||
|
||
val PYTHON_AND_ROBOT_OK_KEY = Key.create<Boolean?>("ROBOTCODE_PYTHON_AND_ROBOT_OK") | ||
} | ||
} | ||
|
||
fun Project.checkPythonAndRobotVersion(reset: Boolean = false): Boolean { | ||
if (!reset && this.getUserData(RobotCodeHelpers.PYTHON_AND_ROBOT_OK_KEY) == true) { | ||
return true | ||
} | ||
|
||
val result = ApplicationManager.getApplication().executeOnPooledThread<Boolean> { | ||
|
||
val pythonInterpreter = this.pythonSdk?.homePath | ||
|
||
if (pythonInterpreter == null) { | ||
thisLogger().info("No Python Interpreter defined for project '${this.name}'") | ||
return@executeOnPooledThread false | ||
} | ||
|
||
if (!Path(pythonInterpreter).exists()) { | ||
thisLogger().warn("Python Interpreter $pythonInterpreter not exists") | ||
return@executeOnPooledThread false | ||
} | ||
|
||
if (!Path(pythonInterpreter).isRegularFile()) { | ||
thisLogger().warn("Python Interpreter $pythonInterpreter is not a regular file") | ||
return@executeOnPooledThread false | ||
} | ||
|
||
thisLogger().info("Use Python Interpreter $pythonInterpreter for project '${this.name}'") | ||
|
||
val res = ExecUtil.execAndGetOutput( | ||
GeneralCommandLine( | ||
pythonInterpreter, "-u", "-c", "import sys; print(sys.version_info[:2]>=(3,8))" | ||
), timeoutInMilliseconds = 5000 | ||
) | ||
if (res.exitCode != 0 || res.stdout.trim() != "True") { | ||
thisLogger().warn("Invalid python version") | ||
return@executeOnPooledThread false | ||
} | ||
|
||
val res1 = ExecUtil.execAndGetOutput( | ||
GeneralCommandLine(pythonInterpreter, "-u", RobotCodeHelpers.checkRobotVersion.pathString), | ||
timeoutInMilliseconds = 5000 | ||
) | ||
if (res1.exitCode != 0 || res1.stdout.trim() != "True") { | ||
thisLogger().warn("Invalid Robot Framework version") | ||
return@executeOnPooledThread false | ||
} | ||
|
||
return@executeOnPooledThread true | ||
|
||
}.get() | ||
|
||
this.putUserData(RobotCodeHelpers.PYTHON_AND_ROBOT_OK_KEY, result) | ||
|
||
return result | ||
} | ||
|
||
|
||
fun Project.buildRobotCodeCommandLine( | ||
args: Array<String> = arrayOf(), | ||
profiles: Array<String> = arrayOf(), | ||
extraArgs: Array<String> = arrayOf(), | ||
format: String = "", | ||
noColor: Boolean = true, | ||
noPager: Boolean = true | ||
): GeneralCommandLine { | ||
if (!this.checkPythonAndRobotVersion()) { | ||
throw IllegalArgumentException("PythonSDK is not defined or robot version is not valid for project ${this.name}") | ||
} | ||
|
||
val pythonInterpreter = this.pythonSdk?.homePath | ||
val commandLine = GeneralCommandLine( | ||
pythonInterpreter, | ||
"-u", | ||
"-X", | ||
"utf8", | ||
RobotCodeHelpers.robotCodePath.pathString, | ||
*(if (format.isNotEmpty()) arrayOf("--format", format) else arrayOf()), | ||
*(if (noColor) arrayOf("--no-color") else arrayOf()), | ||
*(if (noPager) arrayOf("--no-pager") else arrayOf()), | ||
*profiles.flatMap { listOf("--profile", it) }.toTypedArray(), | ||
*extraArgs, | ||
*args | ||
).withWorkDirectory(this.basePath).withCharset(Charsets.UTF_8) | ||
|
||
return commandLine | ||
} |
84 changes: 0 additions & 84 deletions
84
intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/RobotCodeParserDefinition.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/RobotCodeRunConfiguration.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
...llij-client/src/main/kotlin/dev/robotcode/robotcode4ij/RobotCodeTextMateBundleProvider.kt
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/TextMateBundleHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.robotcode.robotcode4ij | ||
|
||
import com.intellij.util.containers.Interner | ||
import org.jetbrains.plugins.textmate.TextMateService | ||
import org.jetbrains.plugins.textmate.language.TextMateLanguageDescriptor | ||
import org.jetbrains.plugins.textmate.language.syntax.TextMateSyntaxTable | ||
|
||
|
||
object TextMateBundleHolder { | ||
private val interner = Interner.createWeakInterner<CharSequence>() | ||
|
||
val descriptor: TextMateLanguageDescriptor by lazy { | ||
|
||
val reader = TextMateService.getInstance().readBundle(RobotCodeHelpers.basePath) | ||
?: throw IllegalStateException("Failed to read robotcode textmate bundle") | ||
|
||
val syntaxTable = TextMateSyntaxTable() | ||
|
||
val grammarIterator = reader.readGrammars().iterator() | ||
while (grammarIterator.hasNext()) { | ||
val grammar = grammarIterator.next() | ||
val rootScopeName = syntaxTable.loadSyntax(grammar.plist.value, interner) ?: continue | ||
if (rootScopeName == "source.robotframework") { | ||
val syntax = syntaxTable.getSyntax(rootScopeName) | ||
return@lazy TextMateLanguageDescriptor(rootScopeName, syntax) | ||
} | ||
} | ||
|
||
throw IllegalStateException("Failed to find robotcode textmate in bundle") | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/actions/RobotCreateFileAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package dev.robotcode.robotcode4ij.actions | ||
|
||
import com.intellij.ide.actions.CreateFileFromTemplateAction | ||
import com.intellij.ide.actions.CreateFileFromTemplateDialog | ||
import com.intellij.ide.fileTemplates.FileTemplateManager | ||
import com.intellij.openapi.project.DumbAware | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiDirectory | ||
import dev.robotcode.robotcode4ij.RobotIcons | ||
import dev.robotcode.robotcode4ij.RobotResourceFileType | ||
import dev.robotcode.robotcode4ij.RobotSuiteFileType | ||
|
||
class RobotCreateFileAction : CreateFileFromTemplateAction( | ||
"Robot Framework File", "Robot Framework file", | ||
RobotIcons | ||
.Suite | ||
), | ||
DumbAware { | ||
override fun buildDialog(project: Project, directory: PsiDirectory, builder: CreateFileFromTemplateDialog.Builder) { | ||
builder.setTitle("New Robot Framework File") | ||
FileTemplateManager.getInstance(project) | ||
.allTemplates | ||
.forEach { | ||
if (it.extension == RobotSuiteFileType.defaultExtension) { | ||
builder.addKind(it.name, RobotIcons.Suite, it.name) | ||
} else if (it.extension == RobotResourceFileType.defaultExtension) { | ||
builder.addKind(it.name, RobotIcons.Resource, it.name) | ||
} | ||
} | ||
builder | ||
.addKind("Suite file", RobotIcons.Suite, "Robot Suite File") | ||
.addKind("Resource file", RobotIcons.Resource, "Robot Resource File") | ||
|
||
} | ||
|
||
override fun getActionName(directory: PsiDirectory?, newName: String, templateName: String?): String { | ||
return "Create Robot Framework File" | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...ent/src/main/kotlin/dev/robotcode/robotcode4ij/editor/RobotCodeCommentTokenSetProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.robotcode.robotcode4ij.editor | ||
|
||
import com.intellij.psi.impl.cache.CommentTokenSetProvider | ||
import com.intellij.psi.tree.IElementType | ||
import dev.robotcode.robotcode4ij.psi.RobotTextMateElementType | ||
|
||
val COMMENT_SCOPES = setOf( | ||
"comment.line.robotframework", | ||
"comment.line.rest.robotframework", | ||
"comment.block.robotframework", | ||
) | ||
|
||
class RobotCodeCommentTokenSetProvider : CommentTokenSetProvider { | ||
override fun isInComments(elementType: IElementType?): Boolean { | ||
val scopeName = (elementType as? RobotTextMateElementType)?.element?.scope?.scopeName | ||
return COMMENT_SCOPES.contains(scopeName) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/editor/RobotCodeCommenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dev.robotcode.robotcode4ij.editor | ||
|
||
import com.intellij.lang.Commenter | ||
|
||
class RobotCodeCommenter : Commenter { | ||
override fun getLineCommentPrefix(): String { | ||
return "#" | ||
} | ||
|
||
override fun getBlockCommentPrefix(): String? { | ||
return null | ||
} | ||
|
||
override fun getBlockCommentSuffix(): String? { | ||
return null | ||
} | ||
|
||
override fun getCommentedBlockCommentPrefix(): String? { | ||
return null | ||
} | ||
|
||
override fun getCommentedBlockCommentSuffix(): String? { | ||
return null | ||
} | ||
} |
Oops, something went wrong.