Skip to content

Commit

Permalink
Add icon in Show Usage popup, fix #105
Browse files Browse the repository at this point in the history
  • Loading branch information
Socolin committed Nov 28, 2023
1 parent d960de1 commit dbdeb03
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Occurrences;

namespace ReSharperPlugin.SpecflowRiderPlugin.Navigation;

[SolutionComponent]
public class SpecflowOccurenceKindProvider : IOccurrenceKindProvider
{
public static readonly OccurrenceKind SpecflowStep = OccurrenceKind.CreateSemantic("SpecflowStep");

public ICollection<OccurrenceKind> GetOccurrenceKinds(IOccurrence occurrence)
{
if (occurrence is SpecflowStepOccurrence)
return [SpecflowStep];
return [];
}

public IEnumerable<OccurrenceKind> GetAllPossibleOccurrenceKinds()
{
return [SpecflowStep];
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using JetBrains.ReSharper.Feature.Services.Occurrences;
using JetBrains.ReSharper.Psi.Search;
using ReSharperPlugin.SpecflowRiderPlugin.References;

namespace ReSharperPlugin.SpecflowRiderPlugin.Navigation;

[OccurrenceProvider(Priority = 10)]
public class SpecflowOccurenceProvider : IOccurrenceProvider
{
public IOccurrence MakeOccurrence(FindResult findResult)
{
if (findResult is FindResultReference findResultReference)
{
if (findResultReference.Reference is SpecflowStepDeclarationReference specflowStepDeclarationReference)
return new SpecflowStepOccurrence(specflowStepDeclarationReference);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using JetBrains.Application;
using JetBrains.Application.Parts;
using JetBrains.ReSharper.Feature.Services.Occurrences;
using JetBrains.UI.Icons;
using ReSharperPlugin.SpecflowRiderPlugin.Icons;

namespace ReSharperPlugin.SpecflowRiderPlugin.Navigation;

[ShellComponent(Instantiation.DemandAnyThread)]
public class SpecflowOccurrenceKindIconProvider : IOccurrenceKindIconProvider
{
public IconId GetImageId(OccurrenceKind declaredElement)
{
if (declaredElement == SpecflowOccurenceKindProvider.SpecflowStep)
return SpecFlowIcons.SpecFlowIcon;
return null;
}

public int GetPriority(OccurrenceKind occurrenceKind)
{
if (occurrenceKind == SpecflowOccurenceKindProvider.SpecflowStep)
return -8;
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using JetBrains.DocumentManagers;
using JetBrains.IDE;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Occurrences;
using JetBrains.ReSharper.Feature.Services.Occurrences.OccurrenceInformation;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Pointers;
using JetBrains.Util;

namespace ReSharperPlugin.SpecflowRiderPlugin.Navigation;

[SolutionFeaturePart]
public class SpecflowStepOccurenceInfoProvider : IOccurrenceInformationProvider2
{
public IDeclaredElementEnvoy GetTypeMember(IOccurrence occurrence)
{
return null;
}

public IDeclaredElementEnvoy GetTypeElement(IOccurrence occurrence)
{
return null;
}

public IDeclaredElementEnvoy GetNamespace(IOccurrence occurrence)
{
return null;
}

public OccurrenceMergeContext GetMergeContext(IOccurrence occurrence)
{
return new OccurrenceMergeContext(occurrence);
}

public TextRange GetTextRange(IOccurrence occurrence)
{
return TextRange.InvalidRange;
}

public ProjectModelElementEnvoy GetProjectModelElementEnvoy(IOccurrence occurrence)
{
var sourceFile = (occurrence as SpecflowStepOccurrence)?.SourceFile;
if (sourceFile != null)
{
var map = sourceFile.GetSolution().GetComponent<DocumentToProjectFileMappingStorage>();
var miscFilesProjectFile = map.TryGetProjectFile(sourceFile.Document);
return miscFilesProjectFile != null ? ProjectModelElementEnvoy.Create(miscFilesProjectFile) : null;
}

return null;
}

public SourceFilePtr GetSourceFilePtr(IOccurrence occurrence)
{
return (occurrence as SpecflowStepOccurrence)?.SourceFile.Ptr() ?? SourceFilePtr.Fake;
}

public bool IsApplicable(IOccurrence occurrence)
{
return occurrence is SpecflowStepOccurrence;
}

public void SetTabOptions(TabOptions tabOptions, IOccurrence occurrence)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using JetBrains.Application.UI.PopupLayout;
using JetBrains.IDE;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Navigation.NavigationExtensions;
using JetBrains.ReSharper.Feature.Services.Occurrences;
using JetBrains.ReSharper.Psi;
using ReSharperPlugin.SpecflowRiderPlugin.References;

namespace ReSharperPlugin.SpecflowRiderPlugin.Navigation;

public sealed class SpecflowStepOccurrence(SpecflowStepDeclarationReference specflowStepDeclarationReference) : IOccurrence
{
public bool Navigate(
ISolution solution,
PopupWindowContextSource windowContext,
bool transferFocus,
TabOptions tabOptions = TabOptions.Default
)
{
specflowStepDeclarationReference.GetTreeNode().NavigateToNode(true);
PresentationOptions = OccurrencePresentationOptions.DefaultOptions;
return true;
}

public ISolution GetSolution()
{
return specflowStepDeclarationReference.GetProject().GetSolution();
}

public string DumpToString()
{
var sb = new StringBuilder();
sb.Append($"SourceFilePtr: = {SourceFile.PsiStorage.PersistentIndex}");
sb.Append($" SpecflowStep: = {specflowStepDeclarationReference.GetStepText()}");
return sb.ToString();
}

public OccurrenceType OccurrenceType => OccurrenceType.Occurrence;
public bool IsValid => SourceFile.IsValid();
public OccurrencePresentationOptions PresentationOptions { get; set; }
public IPsiSourceFile SourceFile => specflowStepDeclarationReference.GetTreeNode().GetSourceFile();

public string GetStepText()
{
return specflowStepDeclarationReference.GetStepText();
}

public string GetScenarioText()
{
return specflowStepDeclarationReference.ScenarioText;
}

[CanBeNull]
public string GetRelatedFilePresentation()
{
return SourceFile.DisplayName.Split('\\').Last();
}

public override string ToString()
{
return GetStepText();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Text;
using JetBrains.Application.UI.Controls.JetPopupMenu;
using JetBrains.ReSharper.Feature.Services.Occurrences;
using JetBrains.UI.RichText;
using JetBrains.Util.Media;
using ReSharperPlugin.SpecflowRiderPlugin.Icons;

namespace ReSharperPlugin.SpecflowRiderPlugin.Navigation;

[OccurrencePresenter(Priority = 10.0)]
public class SpecflowStepOccurrencePresenter : IOccurrencePresenter
{
public bool Present(
IMenuItemDescriptor descriptor,
IOccurrence occurrence,
OccurrencePresentationOptions occurrencePresentationOptions
)
{
if (occurrence is SpecflowStepOccurrence specflowStepOccurrence)
{
var text = new RichText(specflowStepOccurrence.GetStepText(), TextStyle.Default);
descriptor.Text = text;
descriptor.Style = MenuItemStyle.CanExpand | MenuItemStyle.Enabled;
descriptor.Tooltip = specflowStepOccurrence.GetScenarioText();
descriptor.ShortcutText = new RichText(specflowStepOccurrence.GetRelatedFilePresentation(), TextStyle.FromForeColor(JetRgbaColors.DarkGray));
descriptor.Icon = SpecFlowIcons.SpecFlowIcon;
}

return true;
}


public bool IsApplicable(IOccurrence occurrence)
{
return occurrence is SpecflowStepOccurrence;
}
}

0 comments on commit dbdeb03

Please sign in to comment.