Skip to content

Commit b91b537

Browse files
authored
fix code lens when more than one per method (#146)
* fix code lens when more than one per method Signed-off-by: shalom <[email protected]> * fix code lens when more than one per method Signed-off-by: shalom <[email protected]> Signed-off-by: shalom <[email protected]>
1 parent 27bf25b commit b91b537

File tree

13 files changed

+185
-130
lines changed

13 files changed

+185
-130
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pluginUntilBuild=222.*
1212
## supported IDEs : IC,IU,PC,PY,RD,CI
1313
## default is rider
1414
platformType=RD
15-
platformVersion=2022.2
15+
platformVersion=2022.2.1
1616
# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
1717
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:213.7172.25
1818
# to load IC with python support:

ide-common/src/main/java/org/digma/intellij/plugin/document/CodeLensProvider.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ public class CodeLensProvider {
1919
private static final Logger LOGGER = Logger.getInstance(CodeLensProvider.class);
2020

2121
private final DocumentInfoService documentInfoService;
22+
private final Project project;
2223

2324
public CodeLensProvider(Project project) {
2425
documentInfoService = project.getService(DocumentInfoService.class);
26+
this.project = project;
2527
}
2628

2729

@@ -44,11 +46,12 @@ public List<CodeLens> provideCodeLens(@NotNull PsiFile psiFile){
4446
summaries.forEach(codeObjectSummary -> {
4547
switch (codeObjectSummary.getType()){
4648
case MethodSummary:{
49+
4750
MethodCodeObjectSummary methodCodeObjectSummary = (MethodCodeObjectSummary) codeObjectSummary;
4851
int score = methodCodeObjectSummary.getScore();
4952
if (score >= 70){
5053
Log.log(LOGGER::debug, "Collecting code lese for MethodCodeObjectSummary {}",codeObjectSummary.getCodeObjectId());
51-
CodeLens codeLens = new CodeLens(codeObjectSummary.getCodeObjectId(), CodeObjectType.Method,"Error Hotspot");
54+
CodeLens codeLens = new CodeLens(codeObjectSummary.getCodeObjectId(), CodeObjectType.Method, CodeLens.CodeLensType.ErrorHotspot, "Error Hotspot");
5255
codeLens.setLensTooltipText("Error Hotspot for "+codeObjectSummary.getCodeObjectId());
5356
codeLens.setLensMoreText("Go to Error Hotspot");
5457
codeLens.setAnchor("Top");
@@ -59,13 +62,15 @@ public List<CodeLens> provideCodeLens(@NotNull PsiFile psiFile){
5962
break;
6063
}
6164
case EndpointSummary:{
65+
6266
EndpointCodeObjectSummary endpointCodeObjectSummary = (EndpointCodeObjectSummary) codeObjectSummary;
63-
if (endpointCodeObjectSummary.getLowUsage() || endpointCodeObjectSummary.getHighUsage()){
64-
Log.log(LOGGER::debug, "Collecting code lese for EndpointCodeObjectSummary {}",codeObjectSummary.getCodeObjectId());
67+
if (endpointCodeObjectSummary.getLowUsage() || endpointCodeObjectSummary.getHighUsage()) {
68+
Log.log(LOGGER::debug, "Collecting code lese for EndpointCodeObjectSummary {}", codeObjectSummary.getCodeObjectId());
6569
var lensText = endpointCodeObjectSummary.getLowUsage() ? "Low Usage" : "High Usage";
66-
CodeLens codeLens = new CodeLens(codeObjectSummary.getCodeObjectId(), CodeObjectType.Method,lensText);
67-
codeLens.setLensTooltipText("Maximum of "+endpointCodeObjectSummary.getMaxCallsIn1Min() +" requests per minute");
68-
codeLens.setLensMoreText("Go to "+lensText);
70+
var lensType = endpointCodeObjectSummary.getLowUsage() ? CodeLens.CodeLensType.LowUsage : CodeLens.CodeLensType.HighUsage;
71+
CodeLens codeLens = new CodeLens(codeObjectSummary.getCodeObjectId(), CodeObjectType.Method, lensType, lensText);
72+
codeLens.setLensTooltipText("Maximum of " + endpointCodeObjectSummary.getMaxCallsIn1Min() + " requests per minute");
73+
codeLens.setLensMoreText("Go to " + lensText);
6974
codeLens.setAnchor("Top");
7075
codeLensList.add(codeLens);
7176
}

model/src/main/kotlin/org/digma/intellij/plugin/model/lens/CodeLens.kt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@ package org.digma.intellij.plugin.model.lens
22

33
import org.digma.intellij.plugin.model.CodeObjectType
44

5-
data class CodeLens(val codeObjectId: String,
6-
val codeObjectType: CodeObjectType,
7-
val lensText: String,
8-
var lensTooltipText: String,
9-
var lensMoreText: String,
10-
var anchor: String) {
11-
12-
constructor(codeObjectId: String, codeObjectType: CodeObjectType, lensText: String) : this(codeObjectId,codeObjectType,lensText,"","","")
5+
data class CodeLens(
6+
val codeObjectId: String,
7+
val codeObjectType: CodeObjectType,
8+
val type: CodeLensType,
9+
val lensText: String,
10+
var lensTooltipText: String,
11+
var lensMoreText: String,
12+
var anchor: String
13+
) {
14+
15+
constructor(codeObjectId: String, codeObjectType: CodeObjectType, type: CodeLensType, lensText: String) : this(
16+
codeObjectId,
17+
codeObjectType,
18+
type,
19+
lensText,
20+
"",
21+
"",
22+
""
23+
)
24+
25+
26+
enum class CodeLensType {
27+
ErrorHotspot,
28+
LowUsage,
29+
HighUsage
30+
}
1331
}

rider/Digma.Rider.Plugin/Digma.Rider/Highlighting/MethodInsightsProvider.cs renamed to rider/Digma.Rider.Plugin/Digma.Rider/Highlighting/BaseMethodInsightsProvider.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010

1111
namespace Digma.Rider.Highlighting
1212
{
13-
[SolutionComponent]
14-
public class MethodInsightsProvider : ICodeInsightsProvider
13+
public abstract class BaseMethodInsightsProvider : ICodeInsightsProvider
1514
{
1615
private readonly ISolution _solution;
1716
private readonly ILogger _logger;
1817
private readonly ShowToolWindowHost _showToolWindowHost;
1918

20-
public MethodInsightsProvider(ISolution solution,ILogger logger,ShowToolWindowHost showToolWindowHost)
19+
protected BaseMethodInsightsProvider(ISolution solution,ILogger logger,ShowToolWindowHost showToolWindowHost)
2120
{
2221
_solution = solution;
2322
_logger = logger;
@@ -40,7 +39,10 @@ public void OnExtraActionClick(CodeInsightsHighlighting highlighting, string act
4039
Log(_logger, "OnExtraActionClick invoked for {0}", highlighting.DeclaredElement);
4140
NavigateToMethod(highlighting);
4241
}
43-
42+
43+
public abstract string ProviderId { get; }
44+
public abstract string DisplayName { get; }
45+
4446
private void NavigateToMethod(CodeInsightsHighlighting highlighting)
4547
{
4648
using (CompilationContextCookie.GetExplicitUniversalContextIfNotSet())
@@ -51,12 +53,7 @@ private void NavigateToMethod(CodeInsightsHighlighting highlighting)
5153
}
5254

5355

54-
55-
public string ProviderId => nameof(MethodInsightsProvider);
56-
public string DisplayName => "Method Hints";
5756
public CodeLensAnchorKind DefaultAnchor => CodeLensAnchorKind.Top;
58-
59-
public ICollection<CodeLensRelativeOrdering> RelativeOrderings => new CodeLensRelativeOrdering[]
60-
{ new CodeLensRelativeOrderingFirst() };
57+
public abstract ICollection<CodeLensRelativeOrdering> RelativeOrderings { get; }
6158
}
6259
}
Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,69 @@
1+
using System;
2+
using Digma.Rider.Discovery;
13
using Digma.Rider.Protocol;
24
using JetBrains.ProjectModel;
5+
using JetBrains.ReSharper.Daemon.CodeInsights;
36
using JetBrains.ReSharper.Feature.Services.Daemon;
4-
using JetBrains.ReSharper.Psi;
57
using JetBrains.ReSharper.Psi.CSharp.Tree;
8+
using JetBrains.ReSharper.Psi.Tree;
69
using JetBrains.Util;
710
using static Digma.Rider.Logging.Logger;
811

912
namespace Digma.Rider.Highlighting
1013
{
11-
[ElementProblemAnalyzer(typeof(ICSharpFile))]
12-
public class CodeObjectsHighlighter : ElementProblemAnalyzer<ICSharpFile>
14+
[ElementProblemAnalyzer(typeof(ICSharpFunctionDeclaration))]
15+
public class CodeObjectsHighlighter : ElementProblemAnalyzer<ICSharpFunctionDeclaration>
1316
{
1417
private readonly ILogger _logger;
1518
private readonly CodeObjectsHost _codeObjectsHost;
16-
private readonly MethodInsightsProvider _methodInsightsProvider;
19+
private readonly ErrorHotspotMethodInsightsProvider _errorHotspotMethodInsightsProvider;
20+
private readonly UsageMethodInsightsProvider _usageMethodInsightsProvider;
1721

1822
public CodeObjectsHighlighter(ILogger logger,
1923
CodeObjectsHost codeObjectsHost,
2024
ISolution solution,
21-
MethodInsightsProvider methodInsightsProvider)
25+
ErrorHotspotMethodInsightsProvider errorHotspotMethodInsightsProvider,
26+
UsageMethodInsightsProvider usageMethodInsightsProvider)
2227
{
2328
_logger = logger;
2429
_codeObjectsHost = codeObjectsHost;
25-
_methodInsightsProvider = methodInsightsProvider;
30+
_errorHotspotMethodInsightsProvider = errorHotspotMethodInsightsProvider;
31+
_usageMethodInsightsProvider = usageMethodInsightsProvider;
2632
}
2733

28-
protected override void Run(ICSharpFile element,
34+
protected override void Run(ICSharpFunctionDeclaration functionDeclaration,
2935
ElementProblemAnalyzerData data,
30-
IHighlightingConsumer consumer)
36+
IHighlightingConsumer highlightingConsumer)
3137
{
32-
Log(_logger, "CodeObjectsHighlighter.Run invoked for {0}", element.GetSourceFile());
33-
var elementProcessor =
34-
new CodeObjectsHighlightingProcessor(_codeObjectsHost, consumer, _methodInsightsProvider,_logger);
35-
element.ProcessDescendants(elementProcessor);
38+
Log(_logger, "CodeObjectsHighlighter.Run invoked for {0}", functionDeclaration);
39+
40+
var methodFqn = Identities.ComputeFqn(functionDeclaration);
41+
var methodCodeLenses = _codeObjectsHost.GetRiderCodeLensInfo(methodFqn);
42+
if (methodCodeLenses is { Count: > 0 })
43+
{
44+
Log(_logger, "Found {0} code lens for method {1}", methodCodeLenses.Count,methodFqn);
45+
foreach (var riderCodeLensInfo in methodCodeLenses)
46+
{
47+
Log(_logger, "Installing code lens for code method {0}: {1}", methodFqn,riderCodeLensInfo);
48+
49+
ICodeInsightsProvider codeInsightsProvider =
50+
riderCodeLensInfo.Type.Equals(CodeLensType.ErrorHotspot)
51+
? _errorHotspotMethodInsightsProvider
52+
: _usageMethodInsightsProvider;
53+
54+
highlightingConsumer.AddHighlighting(
55+
new CodeInsightsHighlighting(
56+
functionDeclaration.GetNameDocumentRange(),
57+
riderCodeLensInfo.LensText ?? throw new InvalidOperationException("LensText must not be null"),
58+
riderCodeLensInfo.LensTooltip ?? string.Empty, //todo: can be null
59+
riderCodeLensInfo.MoreText ?? string.Empty, //todo: can be null
60+
codeInsightsProvider,
61+
functionDeclaration.DeclaredElement,null)
62+
);
63+
}
64+
65+
}
3666
}
67+
3768
}
3869
}

rider/Digma.Rider.Plugin/Digma.Rider/Highlighting/CodeObjectsHighlightingProcessor.cs

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
using Digma.Rider.Protocol;
3+
using JetBrains.ProjectModel;
4+
using JetBrains.Rider.Model;
5+
using JetBrains.Util;
6+
7+
namespace Digma.Rider.Highlighting
8+
{
9+
[SolutionComponent]
10+
public class ErrorHotspotMethodInsightsProvider : BaseMethodInsightsProvider
11+
{
12+
public ErrorHotspotMethodInsightsProvider(ISolution solution,ILogger logger,ShowToolWindowHost showToolWindowHost)
13+
: base(solution,logger,showToolWindowHost)
14+
{}
15+
16+
17+
public override string ProviderId => nameof(ErrorHotspotMethodInsightsProvider);
18+
public override string DisplayName => "Error Hotspot Method Hints";
19+
20+
public override ICollection<CodeLensRelativeOrdering> RelativeOrderings => new CodeLensRelativeOrdering[]
21+
{ new CodeLensRelativeOrderingFirst() };
22+
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
using Digma.Rider.Protocol;
3+
using JetBrains.ProjectModel;
4+
using JetBrains.Rider.Model;
5+
using JetBrains.Util;
6+
7+
namespace Digma.Rider.Highlighting
8+
{
9+
[SolutionComponent]
10+
public class UsageMethodInsightsProvider : BaseMethodInsightsProvider
11+
{
12+
public UsageMethodInsightsProvider(ISolution solution,ILogger logger,ShowToolWindowHost showToolWindowHost)
13+
: base(solution,logger,showToolWindowHost)
14+
{}
15+
16+
public override string ProviderId => nameof(UsageMethodInsightsProvider);
17+
public override string DisplayName => "Usage Method Hints";
18+
19+
public override ICollection<CodeLensRelativeOrdering> RelativeOrderings => new CodeLensRelativeOrdering[]
20+
{ new CodeLensRelativeOrderingAfter(nameof(ErrorHotspotMethodInsightsProvider)) };
21+
}
22+
}

0 commit comments

Comments
 (0)