Source

Resharper Spell Checker / ReSpeller / Analyzers / AnalyzerHelper.cs

using System;
using System.Collections.Generic;
using System.Text;
using JetBrains.Application;
using JetBrains.DocumentModel;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Naming;
using JetBrains.ReSharper.Psi.Naming.Impl;
using JetBrains.ReSharper.Psi.Naming.Interfaces;
using JetBrains.ReSharper.Psi.Naming.Settings;
using JetBrains.ReSharper.Psi.Tree;
using ReSpeller.Highlightings;
using ReSpeller.SpellEngine;

namespace ReSpeller.Analyzers
{
  static class AnalyzerHelper
  {
    public static void GenerateRangeBasedHighlightings(ISpellChecker checker, IHighlightingConsumer consumer, IFile file, DocumentRange range, 
                                                       Func<DocumentRange, TextRangeTypoHighlighting> highlightingCreator)
    {
      string text = range.GetText();
      List<TextPart> textParts = TextSplitter.Split(text);
      foreach (TextPart part in textParts)
      {
        if (part.Type == TextPartType.Word && !checker.CheckWordSpelling(part.Text))
        {
          DocumentRange partRange = RangeUtils.GetPartRange(range, part);
          consumer.AddHighlighting(highlightingCreator(partRange), partRange, file);
        }
      }      
    }

    public static void GenerateIdentifierHighlightings(IHighlightingConsumer consumer, List<TextPart> innerNameParts, DocumentRange elementRange, string prefix,
                                                       string suffix, IDeclaration declaration,
                                                       bool isNamespace, ITreeNode treeNode)
    {
      var checker = Shell.Instance.GetComponent<ISpellChecker>();
      
      foreach (TextPart p in innerNameParts)
      {
        if (p.Type == TextPartType.Word && p.Text.Length > 2)
        {
          if (!checker.CheckWordSpelling(p.Text))
          {
            DocumentRange partRange = RangeUtils.GetPartRange(elementRange, p, prefix.Length, suffix.Length);
            consumer.AddHighlighting(new IdentifierTypoHighlighting(treeNode, declaration, p, isNamespace),
                                     partRange, treeNode.GetContainingFile());
          }
        }
      }
    }

    public static List<TextPart> DecomposeName(ITreeNode treeNode, IDeclaredElement declaredElement, out string prefix,
                                                out string suffix)
    {
      return DoDecomposeName(treeNode, declaredElement, treeNode.GetText(), out prefix, out suffix);
    }

    public static List<TextPart> DecomposeNamespace(ITreeNode treeNode, INamespaceDeclaration namespaceDeclaration, out string prefix, out string suffix)
    {
      return DoDecomposeName(treeNode, namespaceDeclaration.DeclaredElement, namespaceDeclaration.DeclaredName, out prefix, out suffix);
    }

    public static List<TextPart> DecomposeFullNamespace(ITreeNode treeNode, INamespaceDeclaration namespaceDeclaration, out string prefix, out string suffix)
    {
      return DoDecomposeName(treeNode, namespaceDeclaration.DeclaredElement, namespaceDeclaration.QualifiedName, out prefix, out suffix);
    }

    private static List<TextPart> DoDecomposeName(ITreeNode treeNode, IDeclaredElement declaredElement, string fullName, out string prefix,
                                                out string suffix)
    {     
      IPsiServices psiServices = treeNode.GetPsiServices();
      NamingManager namingManager = psiServices.Naming;
      PsiLanguageType psiLanguageType = treeNode.GetKnownLanguage();
      IPsiSourceFile psiSourceFile = treeNode.GetSourceFile();

      NamingPolicy policy;
      if (declaredElement == null)
      {
        policy = psiServices.Naming.Policy
                            .GetPolicy(NamedElementKinds.Other, psiLanguageType, psiSourceFile);
      }
      else
      {
        policy = psiServices.Naming.Policy
        .GetPolicy(declaredElement, psiLanguageType, psiSourceFile);
      }

      INamingPolicyProvider namingPolicyProvider = psiServices.Naming.Policy
        .GetPolicyProvider(psiLanguageType, psiSourceFile);

      //string shortName = declaration.ShortName;

      Name name = namingManager.Parsing.Parse(fullName, policy.NamingRule, namingPolicyProvider);
      var sb = new StringBuilder(fullName.Length);
      foreach (var innerElement in name.InnerElements)
      {
        sb.Append(innerElement.Text);
      }
      string innerName = sb.ToString();
      List<TextPart> innerNameParts = TextSplitter.Split(innerName);

      prefix = name.NamePrefix.Text;
      suffix = name.NameSuffix.Text;
      return innerNameParts;
    }
  }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.