Example #1
0
        public static async Task <IWpfTextView> GetWpfTextViewAsync(this Microsoft.VisualStudio.Shell.IAsyncServiceProvider serviceProvider)
        {
            IVsTextManager2 textManager = (IVsTextManager2)await serviceProvider.GetServiceAsync(typeof(SVsTextManager));

            if (textManager == null)
            {
                return(null);
            }

            int result = textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out IVsTextView view);

            if (result != VSConstants.S_OK)
            {
                return(null);
            }

            IComponentModel componentModel = (IComponentModel)await serviceProvider.GetServiceAsync(typeof(SComponentModel));

            IVsEditorAdaptersFactoryService adapterService = componentModel?.GetService <IVsEditorAdaptersFactoryService>();

            if (adapterService == null)
            {
                return(null);
            }

            return(adapterService.GetWpfTextView(view));
        }
        public static string GetHighlightedText(IVsTextManager2 textManager)
        {
            if (textManager == null)
            {
                throw new Exception("Could not locate the TextManager service.");
            }

            textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out IVsTextView view);

            view.GetSelectedText(out string selectedText);

            return(selectedText);
        }
Example #3
0
        private async void Execute(object sender, EventArgs e)
        {
            object service = await ServiceProvider.GetServiceAsync(typeof(SVsTextManager));

            IVsTextManager2 textManager = service as IVsTextManager2;

            _ = textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out IVsTextView view);

            view.GetSelection(out _, out _, out _, out _); //end could be before beginning
            view.GetSelectedText(out string selectedText);

            StatementSyntax syntax = SyntaxFactory.ParseStatement(selectedText);

            SyntaxNode formattedResult = Formatter.Format(syntax, new AdhocWorkspace());

            SyntaxSenderService.Send(formattedResult, Language);
        }
Example #4
0
        public static IEnumerable <string> GetSelection(EmptyLineAction Action, out int[] EmptyLinePositions, out bool WasNewLine)
        {
            IVsTextManager2 textManager = ServiceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager2;

            Assumes.Present(textManager);
            textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out IVsTextView view);
            view.GetSelectedText(out string selectedText);
            WasNewLine = selectedText.EndsWith("\n");
            IEnumerable <string> result = selectedText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            int lastIndex = ((string[])result).Length - 1;

            EmptyLinePositions = result.Select((x, i) => new { Line = x, Index = i }).Where(x => string.IsNullOrWhiteSpace(x.Line)).Select(x => x.Index).ToArray();
            if (Action == EmptyLineAction.DependsOnSettings)
            {
                Action = VSPackage.Loader.Settings.EmptyLineAction;
            }
            result = Action == EmptyLineAction.AsLine ? result.Where((x, i) => !(string.IsNullOrEmpty(x) && i == lastIndex)) : result.Where(x => !string.IsNullOrWhiteSpace(x));
            return(result);
        }
Example #5
0
        public void Execute(object sender, EventArgs e)
        {
            textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftAny, out IVsTextView textView);
            textView.GetCaretPos(out int line, out int index);

            IVsTextLines textLines;

            textView.GetBuffer(out textLines);
            string buffer;
            int    lineLength;

            textLines.GetLengthOfLine(line, out lineLength);
            textLines.GetLineText(line, 0, line, lineLength, out buffer);
            while (lineLength > 0 && buffer[lineLength - 1] == ' ')
            {
                lineLength -= 1;
            }
            int frontWhitespaces = 0;

            while (frontWhitespaces < lineLength && buffer[frontWhitespaces] == ' ')
            {
                frontWhitespaces += 1;
            }

            int  wordsCount = 1;
            bool includeWhitespacesInWord = false;

            if (previousCommand == 0b1)
            {
                //wordsCount += 1;
                includeWhitespacesInWord = true;
            }
            previousCommand = ((previousCommand << 1) + 1) & 1;
            if (index >= lineLength)
            {
                if (lineLength == 0)
                {
                    dte.ExecuteCommand("Edit.LineEnd");
                }
                else
                {
                    index = frontWhitespaces;
                    textView.SetCaretPos(line, index);
                }
                return;
            }

            TextSpan[] span = new TextSpan[1];
            for (; index < lineLength && wordsCount != 0; wordsCount -= 1)
            {
                if (buffer[index] == ' ')
                {
                    while (index < lineLength && buffer[index] == ' ')
                    {
                        index += 1;
                    }
                    continue;
                }

                textView.GetWordExtent(line, index, (uint)WORDEXTFLAGS.WORDEXT_NEXT + (uint)WORDEXTFLAGS.WORDEXT_FINDTOKEN, span);
                if (span[0].iStartIndex != span[0].iEndIndex)
                {
                    if (index == span[0].iEndIndex)
                    {
                        index += 1;
                    }
                    else
                    {
                        index = span[0].iEndIndex;
                    }
                }
                else
                {
                    index += 1;
                }

                if (includeWhitespacesInWord)
                {
                    while (index < lineLength && buffer[index] == ' ')
                    {
                        index += 1;
                    }
                }
                continue;
            }
            textView.SetCaretPos(line, index);
        }
Example #6
0
        private void Execute(object sender, EventArgs e)
        {
            textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftAny, out IVsTextView textView);
            textView.GetCaretPos(out int line, out int index);

            IVsTextLines textLines;

            textView.GetBuffer(out textLines);
            string buffer;
            int    lineLength;

            textLines.GetLengthOfLine(line, out lineLength);
            textLines.GetLineText(line, 0, line, lineLength, out buffer);
            while (lineLength > 0 && buffer[lineLength - 1] == ' ')
            {
                lineLength -= 1;
            }
            int frontWhitespaces = 0;

            while (frontWhitespaces < lineLength && buffer[frontWhitespaces] == ' ')
            {
                frontWhitespaces += 1;
            }

            int wordsCount          = 1;
            int whitespaceWordCount = 0;

            if (NextWordCommand.previousCommand == 0b0)
            {
                whitespaceWordCount += 1;
            }
            NextWordCommand.previousCommand = ((NextWordCommand.previousCommand << 1) + 0) & 1;

            if (index > lineLength)
            {
                index = lineLength;
            }

            if (index <= frontWhitespaces)
            {
                if (lineLength == 0)
                {
                    dte.ExecuteCommand("Edit.LineEnd");
                }
                else
                {
                    index = lineLength;
                    textView.SetCaretPos(line, index);
                }
                return;
            }

            TextSpan[] span = new TextSpan[1];
            for (; index > frontWhitespaces && wordsCount != 0; wordsCount -= 1)
            {
                if (buffer[index - 1] == ' ')
                {
                    while (index > frontWhitespaces && buffer[index - 1] == ' ')
                    {
                        index -= 1;
                    }
                    if (whitespaceWordCount > 0)
                    {
                        --whitespaceWordCount;
                    }
                    else
                    {
                        continue;
                    }
                }

                index -= 1;
                textView.GetWordExtent(line, index, (uint)WORDEXTFLAGS.WORDEXT_PREVIOUS + (uint)WORDEXTFLAGS.WORDEXT_FINDTOKEN, span);
                if (span[0].iStartIndex != span[0].iEndIndex)
                {
                    if (index != span[0].iEndIndex)
                    {
                        index = span[0].iStartIndex;
                    }
                }

                continue;
            }

            textView.SetCaretPos(line, index);
        }