Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use ruby sdk to find haml-lint executable #107

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pluginUntilBuild = 243.*

# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType = IU
platformVersion = 2024.2
platformVersion = 2024.3

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.jetbrains.php:203.4449.22, org.intellij.scala:2023.3.27@EAP
platformPlugins = org.jetbrains.plugins.haml:242.20224.175
platformPlugins = org.jetbrains.plugins.haml:243.21565.122, org.jetbrains.plugins.ruby:243.21565.211
# Example: platformBundledPlugins = com.intellij.java
platformBundledPlugins =

Expand Down
45 changes: 25 additions & 20 deletions src/main/kotlin/me/benmelz/jetbrains/plugins/hamllint/HamlLint.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.benmelz.jetbrains.plugins.hamllint

import com.google.gson.JsonParser
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.process.OSProcessHandler
import com.intellij.execution.process.ProcessHandler
import com.intellij.execution.process.ScriptRunnerUtil
import java.io.File
import java.nio.charset.StandardCharsets
import com.intellij.openapi.module.ModuleUtil
import com.intellij.openapi.project.ProjectLocator
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vfs.LocalFileSystem
import org.jetbrains.plugins.ruby.gem.RubyGemExecutionContext
import java.nio.file.Path
import java.util.LinkedList

Expand All @@ -15,18 +17,15 @@ import java.util.LinkedList
* @param[haml] raw haml code to lint.
* @param[filePath] path to the file that is being linted.
* @param[workDirectory] work directory from which to run haml-lint.
* @param[executionCommand] execution command with which to run haml-lint.
* @return a list of collected haml-lint offenses.
*/
fun hamlLint(
haml: CharSequence,
filePath: Path,
workDirectory: Path,
executionCommand: List<String>,
): List<HamlLintOffense> {
val cli = hamlLintCommandLine(filePath, workDirectory, executionCommand)
val processHandler = OSProcessHandler(cli)
val stdin = processHandler.processInput
val processHandler = hamlLintProcessHandler(filePath, workDirectory)
val stdin = processHandler.processInput!!
haml.forEach { stdin.write(it.code) }
stdin.close()
return parseHamlLintOutput(
Expand All @@ -35,23 +34,29 @@ fun hamlLint(
}

/**
* Builds an executable [GeneralCommandLine] that runs `haml-lint` in stdin mode and the json reporter.
* Builds an executable [ProcessHandler] that runs `haml-lint` in stdin mode and the json reporter.
*
* @param[filePath] original file path for the code that is being linted.
* @param[workDirectory] the directory from which to run `haml-lint`.
* @param[executionCommand] the execution command with which to run haml-lint.
* @return an executable [GeneralCommandLine].
* @return an executable [ProcessHandler].
*/
private fun hamlLintCommandLine(
private fun hamlLintProcessHandler(
filePath: Path,
workDirectory: Path,
executionCommand: List<String>,
): GeneralCommandLine =
GeneralCommandLine(executionCommand).apply {
this.addParameters("--stdin", filePath.toString(), "--reporter", "json")
this.charset = StandardCharsets.UTF_8
this.workDirectory = File(workDirectory.toUri())
}
): ProcessHandler {
val virtualFile = LocalFileSystem.getInstance().findFileByPath(filePath.toString())!!
val project = ProjectLocator.getInstance().guessProjectForFile(virtualFile)!!
val module = ModuleUtil.findModuleForFile(virtualFile, project)!!
val moduleManager = ModuleRootManager.getInstance(module)
val sdk = moduleManager.sdk!!
val executionContext =
RubyGemExecutionContext
.create(sdk, "haml-lint")
.withModule(module)
.withWorkingDirPath(workDirectory.toString())
.withArguments("--stdin", filePath.toString(), "--reporter", "json")
return executionContext.toRubyScriptExecutionContext()!!.createProcessHandler()!!
}

/**
* Parses the output of a `haml-lint` run using the `json` reporter as [HamlLintOffense]s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ package me.benmelz.jetbrains.plugins.hamllint
* @property[enabled] whether or not inspection is enabled.
* @property[errorSeverityKey] The name of the highlight severity to use for errors.
* @property[warningSeverityKey] The name of the highlight severity to use for errors.
* @property[executionCommand] The command to execute with.
*/
data class HamlLintConfiguration(
val enabled: Boolean,
var errorSeverityKey: String,
var warningSeverityKey: String,
var executionCommand: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ class HamlLintExternalAnnotator : ExternalAnnotator<HamlLintExternalAnnotatorInf
ProjectFileIndex.getInstance(file.project).getContentRootForFile(file.virtualFile)?.toNioPath()
?: return null

val executionCommand = configuration.executionCommand.split(" ").filter { it.isNotEmpty() }
if (executionCommand.isEmpty()) return null

return HamlLintExternalAnnotatorInfo(fileText, filePath, contentRoot, executionCommand)
return HamlLintExternalAnnotatorInfo(fileText, filePath, contentRoot)
}

/**
Expand All @@ -60,7 +57,6 @@ class HamlLintExternalAnnotator : ExternalAnnotator<HamlLintExternalAnnotatorInf
collectedInfo.fileText,
collectedInfo.filePath,
collectedInfo.contentRoot,
collectedInfo.executionCommand,
)
} catch (e: ExecutionException) {
logger.error(e.message)
Expand Down Expand Up @@ -105,7 +101,6 @@ class HamlLintExternalAnnotator : ExternalAnnotator<HamlLintExternalAnnotatorInf
inspectionTool.isEnabled,
inspectionProfileEntry.errorSeverityKey,
inspectionProfileEntry.warningSeverityKey,
inspectionProfileEntry.executionCommand,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import java.nio.file.Path
* @property[fileText] raw `haml` code to lint.
* @property[filePath] path to the file that is to be linted.
* @property[contentRoot] directory of the parent project of the code to be linted.
* @param[executionCommand] execution command with which to run haml-lint.
*/
data class HamlLintExternalAnnotatorInfo(
val fileText: CharSequence,
val filePath: Path,
val contentRoot: Path,
val executionCommand: List<String>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import com.intellij.codeInspection.options.OptPane.dropdown
import com.intellij.codeInspection.options.OptPane.group
import com.intellij.codeInspection.options.OptPane.option
import com.intellij.codeInspection.options.OptPane.pane
import com.intellij.codeInspection.options.OptPane.separator
import com.intellij.codeInspection.options.OptPane.string
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.psi.PsiElementVisitor

Expand All @@ -31,11 +29,6 @@ class HamlLintInspection : LocalInspectionTool() {
*/
var warningSeverityKey: String = HighlightSeverity.WEAK_WARNING.name

/**
* The execution command with which to run `haml-lint`.
*/
var executionCommand: String = "bundle exec haml-lint"

/**
* Delegates inspection logic to a [HamlLintExternalAnnotator].
*
Expand Down Expand Up @@ -76,11 +69,6 @@ class HamlLintInspection : LocalInspectionTool() {
dropdown("errorSeverityKey", "Error: ", *severityOptions),
dropdown("warningSeverityKey", "Warning: ", *severityOptions),
),
separator(),
group(
"Execution Command",
string("executionCommand", "", 32),
),
)
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<depends>com.intellij.modules.platform</depends>
<depends>org.jetbrains.plugins.haml</depends>
<depends>org.jetbrains.plugins.ruby</depends>

<extensions defaultExtensionNs="com.intellij">
<localInspection language="Haml"
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/inspectionDescriptions/HamlLint.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
<p>
Use the <em>Severities Mapping</em> fields to customize the highlight levels.
</p>
<p>
Use the <em>Execution Command</em> field to customize how <code>haml-lint</code> will be invoked.
</p>
</body>
</html>
Loading