Ejemplo n.º 1
0
        public override ITreeRange Format(
            ITreeNode firstElement,
            ITreeNode lastElement,
            CodeFormatProfile profile,
            IProgressIndicator pi,
            IContextBoundSettingsStore overrideSettingsStore = null)
        {
            ITreeNode firstNode;
            ITreeNode lastNode;

            GetFirstAndLastNode(firstElement, lastElement, out firstNode, out lastNode);

            var contextBoundSettingsStore = this.GetProperContextBoundSettingsStore(overrideSettingsStore, firstNode);

            var globalSettings = GlobalFormatSettingsHelper
                                 .GetService(firstNode.GetSolution())
                                 .GetSettingsForLanguage(this.myLanguage, firstNode.GetProjectFileType(), contextBoundSettingsStore);

            using (pi.SafeTotal(4))
            {
                var context = new PsiCodeFormattingContext(this, firstNode, lastNode);
                if (profile != CodeFormatProfile.INDENT)
                {
                    using (IProgressIndicator subPi = pi.CreateSubProgress(2))
                    {
                        using (subPi.SafeTotal(2))
                        {
                            SecretFormattingStage.DoFormat(context, subPi.CreateSubProgress(1));
                            SecretIndentingStage.DoIndent(context, globalSettings, subPi.CreateSubProgress(1), false);
                        }
                    }
                }
                else
                {
                    using (IProgressIndicator subPi = pi.CreateSubProgress(4))
                    {
                        SecretIndentingStage.DoIndent(context, globalSettings, subPi, true);
                    }
                }
            }
            return(new TreeRange(firstElement, lastElement));
        }
Ejemplo n.º 2
0
        protected static void CheckAndIndent(ISolution solution, IProjectFile projectFile, ITextControl textControl, TextRange abbrRange, string expanded, int insertPoint)
        {
            if (expanded.IsEmpty())
            {
                Win32Declarations.MessageBeep(MessageBeepType.Error);
                return;
            }

            /*var indentSize = GlobalFormatSettingsHelper.GetService(solution)
             * .GetSettingsForLanguage(PsiProjectFileTypeCoordinator.Instance.GetPrimaryPsiLanguageType(projectFile), projectFile.LanguageType, false ).IndentSize;*/
            var indentSize = GlobalFormatSettingsHelper.GetService(solution)
                             .GetSettingsForLanguage(PsiProjectFileTypeCoordinator.Instance.GetPrimaryPsiLanguageType(projectFile)).IndentSize;

            expanded = GetEngine(solution).PadString(expanded, (int)textControl.Document.GetCoordsByOffset(abbrRange.StartOffset).Column / indentSize);
            textControl.Document.ReplaceText(abbrRange, expanded);
            if (insertPoint != -1)
            {
                textControl.Caret.MoveTo(abbrRange.StartOffset + insertPoint, CaretVisualPlacement.Generic);
            }
        }
Ejemplo n.º 3
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));
        }