Skip to content

Commit

Permalink
Adds support for the run button on main functions in CLion nova (#7100)
Browse files Browse the repository at this point in the history
Adds support for the gutter icon on main functions in C and CPP files. However, only for nova since the backend does all the heavy lifting for us.
  • Loading branch information
LeFrosch authored Dec 16, 2024
1 parent 6db6a26 commit 7a84d8b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2024 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.clwb.radler

import com.google.idea.blaze.base.dependencies.TargetInfo
import com.google.idea.blaze.base.model.primitives.RuleType
import com.google.idea.blaze.base.run.SourceToTargetFinder
import com.google.idea.blaze.base.run.producers.BinaryContextProvider
import com.google.idea.blaze.base.run.producers.BinaryContextProvider.BinaryRunContext
import com.google.idea.blaze.cpp.CppBlazeRules.RuleTypes
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.psi.PsiElement
import com.intellij.psi.impl.source.tree.LeafElement
import com.jetbrains.cidr.radler.protocol.RadSymbolsHost
import java.io.File
import java.util.*

/**
* This run configuration provider creates configurations for the gutter icon created by the
* [com.jetbrains.cidr.cpp.runFile.nova.CppFileNovaRunLineMarkerProvider], since the actual gutter icon provider for
* radler requires a [com.jetbrains.cidr.execution.CidrTargetRunConfigurationProducer] and it would not be feasible to
* implement one here.
*/
class RadBinaryContextProvider : BinaryContextProvider {

override fun getRunContext(context: ConfigurationContext): BinaryRunContext? {
if (!isMain(context.psiLocation)) return null

val target = findTargets(context).firstOrNull() ?: return null

return BinaryRunContext.create(context.psiLocation, target)
}
}

private fun isMain(element: PsiElement?): Boolean {
if (element !is LeafElement) return false

val symbolsHost = RadSymbolsHost.getInstance(element.project)
return symbolsHost.isEntryPointOffset(element.containingFile.viewProvider.virtualFile, element.startOffset)
}

private fun findTargets(context: ConfigurationContext): Collection<TargetInfo> {
val virtualFile = context.location?.virtualFile ?: return emptyList()

val targets = SourceToTargetFinder.findTargetsForSourceFile(
context.project,
virtualFile.toNioPath().toFile(),
Optional.of(RuleType.BINARY),
) ?: return emptyList()

return targets.filter { it -> it.kind == RuleTypes.CC_BINARY.kind }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<idea-plugin>
<extensions defaultExtensionNs="com.google.idea.blaze">
<TestContextProvider implementation="com.google.idea.blaze.clwb.radler.RadGoogleTestContextProvider"/>
<BinaryContextProvider implementation="com.google.idea.blaze.clwb.radler.RadBinaryContextProvider"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public class NonBlazeProducerSuppressor implements StartupActivity.DumbAware {

// Gutter icons in CMake files.
"com.jetbrains.cidr.cpp.execution.CMakeTargetRunConfigurationProducer",
"com.jetbrains.cidr.cpp.execution.debugger.CMakeRunConfigurationProducer",

// Gutter icons that sometimes appear in `cpp` files with a `main` function.
"com.jetbrains.cidr.cpp.runfile.CppFileTargetRunConfigurationProducer"
"com.jetbrains.cidr.cpp.execution.debugger.CMakeRunConfigurationProducer"
);

@Override
Expand Down

0 comments on commit 7a84d8b

Please sign in to comment.