コード例 #1
0
        public PsiFormattingVisitor(FormattingStageData data)
        {
            ITreeNode      node        = data.Context.FirstNode;
            IPsiSourceFile projectFile = node.GetSourceFile();

            if (projectFile != null)
            {
                myIsGenerated = !Equals(projectFile.PrimaryPsiLanguage, node.Language);
            }
        }
コード例 #2
0
    private static PsiFormattingVisitor CreateFormattingVisitor(FormattingStageData data)
    {
      IPsiSourceFile sourceFile = data.Context.FirstNode.GetSourceFile();
      if (sourceFile != null)
      {
        var projectFileTypeServices = Shell.Instance.GetComponent<IProjectFileTypeServices>();
        var factory = projectFileTypeServices.TryGetService<IPsiCodeFormatterFactory>(sourceFile.LanguageType);
        if (factory != null)
        {
          return factory.CreateFormattingVisitor(data);
        }
      }

      return new PsiFormattingVisitor(data);
    }
コード例 #3
0
    public static void DoFormat(FormattingStageData data, IProgressIndicator pi)
    {
      if (data.Context.FirstNode == data.Context.LastNode)
      {
        return;
      }

      var stage = new PsiFormattingStage(data);

      IEnumerable<FormattingRange> nodePairs = GetNodePairs(data.Context);

      IEnumerable<FormatResult<IEnumerable<string>>> spaces = nodePairs.Select(
        range => new FormatResult<IEnumerable<string>>(range, stage.CalcSpaces(new PsiFmtStageContext(range))));

      FormatterImplHelper.ForeachResult(spaces, pi, res => stage.MakeFormat(res.Range, res.ResultValue));
    }
コード例 #4
0
        public static void DoFormat(FormattingStageData data, IProgressIndicator pi)
        {
            if (data.Context.FirstNode == data.Context.LastNode)
            {
                return;
            }

            var stage = new PsiFormattingStage(data);

            IEnumerable <FormattingRange> nodePairs = GetNodePairs(data.Context);

            IEnumerable <FormatResult <IEnumerable <string> > > spaces = nodePairs.Select(
                range => new FormatResult <IEnumerable <string> >(range, stage.CalcSpaces(new PsiFmtStageContext(range))));

            FormatterImplHelper.ForeachResult(spaces, pi, res => stage.MakeFormat(res.Range, res.ResultValue));
        }
コード例 #5
0
        private static PsiFormattingVisitor CreateFormattingVisitor(FormattingStageData data)
        {
            IPsiSourceFile sourceFile = data.Context.FirstNode.GetSourceFile();

            if (sourceFile != null)
            {
                var projectFileTypeServices = Shell.Instance.GetComponent <IProjectFileTypeServices>();
                var factory = projectFileTypeServices.TryGetService <IPsiCodeFormatterFactory>(sourceFile.LanguageType);
                if (factory != null)
                {
                    return(factory.CreateFormattingVisitor(data));
                }
            }

            return(new PsiFormattingVisitor(data));
        }
コード例 #6
0
        public override ITreeRange Format(ITreeNode firstElement, ITreeNode lastElement, CodeFormatProfile profile, [CanBeNull] IProgressIndicator pi, IContextBoundSettingsStore overrideSettingsStore = null)
        {
            var       psiProfile = new PsiFormatProfile(profile);
            ITreeNode firstNode  = firstElement;
            ITreeNode lastNode   = lastElement;

            if (firstElement != lastElement)
            {
                if (firstElement == null)
                {
                    firstNode = lastElement;
                }
                ITreeNode commonParent = firstNode.FindCommonParent(lastNode);
                ITreeNode firstChild   = firstNode;
                ITreeNode lastChild    = lastElement;
                while (firstChild.Parent != commonParent)
                {
                    firstChild = firstChild.Parent;
                }
                while (lastChild.Parent != commonParent)
                {
                    lastChild = lastChild.Parent;
                }

                bool hasPrevSibling = false;
                while (firstNode.NextSibling == null)
                {
                    if (firstNode.PrevSibling != null)
                    {
                        hasPrevSibling = true;
                    }
                    firstNode = firstNode.Parent;
                    if (firstNode == firstChild)
                    {
                        break;
                    }
                }
                if (hasPrevSibling)
                {
                    while ((firstNode == firstChild) || (firstChild.IsWhitespaceToken()))
                    {
                        firstChild = firstChild.NextSibling;
                    }
                }
                firstNode = firstChild;
                while (firstNode.FirstChild != null)
                {
                    firstNode = firstNode.FirstChild;
                }
            }
            else
            {
                if (firstElement.FirstChild != null)
                {
                    firstNode = firstElement.FirstChild;
                    lastNode  = firstElement.LastChild;
                }
            }
            ISolution            solution = firstNode.GetSolution();
            var                  contextBoundSettingsStore = GetProperContextBoundSettingsStore(overrideSettingsStore, firstNode);
            GlobalFormatSettings globalSettings            = GlobalFormatSettingsHelper.GetService(solution).GetSettingsForLanguage(myLanguage);
            //var formatterSettings = new PsiCodeFormattingSettings(globalSettings, contextBoundSettingsStore.GetKey<CommonFormatterSettingsKey>(mySettingsOptimization));
            var formatterSettings = new PsiCodeFormattingSettings(globalSettings);

            using (pi.SafeTotal(4))
            {
                var context = new PsiCodeFormattingContext(this, firstNode, lastNode, NullProgressIndicator.Instance);
                if (psiProfile.Profile != CodeFormatProfile.INDENT)
                {
                    using (pi.CreateSubProgress(1))
                    {
                        //FormatterImplHelper.DecoratingIterateNodes(context, context.FirstNode, context.LastNode, new PsiDecorationStage(formatterSettings, psiProfile, subPi));
                    }

                    using (IProgressIndicator subPi = pi.CreateSubProgress(1))
                    {
                        using (subPi.SafeTotal(2))
                        {
                            var data = new FormattingStageData(formatterSettings, context, psiProfile, myExtensions.ToList());
                            PsiFormattingStage.DoFormat(data, subPi.CreateSubProgress(1));
                            PsiIndentingStage.DoIndent(formatterSettings, context, subPi.CreateSubProgress(1), false);
                        }
                    }
                }
                else
                {
                    using (IProgressIndicator subPi = pi.CreateSubProgress(4))
                    {
                        PsiIndentingStage.DoIndent(formatterSettings, context, subPi, true);
                    }
                }
            }
            return(new TreeRange(firstElement, lastElement));
        }
コード例 #7
0
    public override ITreeRange Format(ITreeNode firstElement, ITreeNode lastElement, CodeFormatProfile profile, [CanBeNull] IProgressIndicator pi, IContextBoundSettingsStore overrideSettingsStore = null)
    {
      var psiProfile = new PsiFormatProfile(profile);
      ITreeNode firstNode = firstElement;
      ITreeNode lastNode = lastElement;
      if (firstElement != lastElement)
      {
        if (firstElement == null)
        {
          firstNode = lastElement;
        }
        ITreeNode commonParent = firstNode.FindCommonParent(lastNode);
        ITreeNode firstChild = firstNode;
        ITreeNode lastChild = lastElement;
        while (firstChild.Parent != commonParent)
        {
          firstChild = firstChild.Parent;
        }
        while (lastChild.Parent != commonParent)
        {
          lastChild = lastChild.Parent;
        }

        bool hasPrevSibling = false;
        while (firstNode.NextSibling == null)
        {
          if (firstNode.PrevSibling != null)
          {
            hasPrevSibling = true;
          }
          firstNode = firstNode.Parent;
          if (firstNode == firstChild)
          {
            break;
          }
        }
        if (hasPrevSibling)
        {
          while ((firstNode == firstChild) || (firstChild.IsWhitespaceToken()))
          {
            firstChild = firstChild.NextSibling;
          }
        }
        firstNode = firstChild;
        while (firstNode.FirstChild != null)
        {
          firstNode = firstNode.FirstChild;
        }
      } else
      {
        if (firstElement.FirstChild != null)
        {
          firstNode = firstElement.FirstChild;
          lastNode = firstElement.LastChild;
        }
      }
      ISolution solution = firstNode.GetSolution();
      var contextBoundSettingsStore = GetProperContextBoundSettingsStore(overrideSettingsStore, firstNode);
      GlobalFormatSettings globalSettings = GlobalFormatSettingsHelper.GetService(solution).GetSettingsForLanguage(myLanguage);
      //var formatterSettings = new PsiCodeFormattingSettings(globalSettings, contextBoundSettingsStore.GetKey<CommonFormatterSettingsKey>(mySettingsOptimization));
      var formatterSettings = new PsiCodeFormattingSettings(globalSettings);
      using (pi.SafeTotal(4))
      {
        var context = new PsiCodeFormattingContext(this, firstNode, lastNode, NullProgressIndicator.Instance);
        if (psiProfile.Profile != CodeFormatProfile.INDENT)
        {
          using (pi.CreateSubProgress(1))
          {
            //FormatterImplHelper.DecoratingIterateNodes(context, context.FirstNode, context.LastNode, new PsiDecorationStage(formatterSettings, psiProfile, subPi));
          }

          using (IProgressIndicator subPi = pi.CreateSubProgress(1))
          {
            using (subPi.SafeTotal(2))
            {
              var data = new FormattingStageData(formatterSettings, context, psiProfile, myExtensions.ToList());
              PsiFormattingStage.DoFormat(data, subPi.CreateSubProgress(1));
              PsiIndentingStage.DoIndent(formatterSettings, context, subPi.CreateSubProgress(1), false);
            }
          }
        }
        else
        {
          using (IProgressIndicator subPi = pi.CreateSubProgress(4))
          {
            PsiIndentingStage.DoIndent(formatterSettings, context, subPi, true);
          }
        }
      }
      return new TreeRange(firstElement, lastElement);
    }
コード例 #8
0
 public PsiFormattingVisitor CreateFormattingVisitor(FormattingStageData formattingData)
 {
     return(new PsiFormattingVisitor(formattingData));
 }
コード例 #9
0
 private PsiFormattingStage(FormattingStageData data)
 {
   myContext = data.Context;
   myFmtVisitor = CreateFormattingVisitor(data);
 }
コード例 #10
0
 public PsiFormattingVisitor CreateFormattingVisitor(FormattingStageData formattingData)
 {
   return new PsiFormattingVisitor(formattingData);
 }
コード例 #11
0
 private PsiFormattingStage(FormattingStageData data)
 {
     myContext    = data.Context;
     myFmtVisitor = CreateFormattingVisitor(data);
 }