Ejemplo n.º 1
0
        private static bool IsInTokenTypesArea(int position, TextArea textArea, EdgeTrackingMode edgeTrackingMode, params PSTokenType[] selectedPSTokenTypes)
        {
            if (position < 0 || position > textArea.Document.Text.Length)
            {
                return(false);
            }

            var scriptToCaret = string.Empty;

            Execute.OnUIThread(() =>
            {
                scriptToCaret = textArea.Document.Text.Substring(0, textArea.Caret.Offset);
            });

            Token[]      tokens;
            ParseError[] errors;
            System.Management.Automation.Language.Parser.ParseInput(scriptToCaret, out tokens, out errors);

            if (tokens.Length > 0)
            {
                var filteredTokens = tokens.Where(t => selectedPSTokenTypes.Any(k => PSToken.GetPSTokenType(t) == k)).ToList();

                switch (edgeTrackingMode)
                {
                case EdgeTrackingMode.NoneEdgeIncluded:
                    foreach (var token in filteredTokens)
                    {
                        if (token.Extent.StartOffset < position && position < token.Extent.EndOffset)
                        {
                            return(true);
                        }

                        if (position <= token.Extent.StartOffset)
                        {
                            return(false);
                        }
                    }
                    break;

                case EdgeTrackingMode.LeftEdgeIncluded:
                    foreach (var token in filteredTokens)
                    {
                        if (token.Extent.StartOffset <= position && position < token.Extent.EndOffset)
                        {
                            return(true);
                        }

                        if (position < token.Extent.StartOffset)
                        {
                            return(false);
                        }
                    }
                    break;

                case EdgeTrackingMode.RightEdgeIncluded:
                    foreach (var token in filteredTokens)
                    {
                        if (token.Extent.StartOffset < position && position <= token.Extent.EndOffset)
                        {
                            return(true);
                        }

                        if (position < token.Extent.StartOffset)
                        {
                            return(false);
                        }
                    }
                    break;

                case EdgeTrackingMode.BothEdgesIncluded:
                    foreach (var token in filteredTokens)
                    {
                        if (token.Extent.StartOffset <= position && position <= token.Extent.EndOffset)
                        {
                            return(true);
                        }

                        if (position < token.Extent.StartOffset)
                        {
                            return(false);
                        }
                    }
                    break;
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        private static bool IsInCertainPSTokenTypesArea(int position, ITextBuffer buffer, EdgeTrackingMode edgeTrackingMode, params PSTokenType[] selectedPSTokenTypes)
        {
            if (position < 0 || position > buffer.CurrentSnapshot.Length)
            {
                return(false);
            }

            Token[] tokens;
            if (buffer.Properties.TryGetProperty <Token[]>(BufferProperties.Tokens, out tokens) && tokens != null && tokens.Length != 0)
            {
                var filteredTokens = tokens.Where(t => selectedPSTokenTypes.Any(k => PSToken.GetPSTokenType(t) == k)).ToList();

                switch (edgeTrackingMode)
                {
                case EdgeTrackingMode.NoneEdgeIncluded:
                    foreach (var token in filteredTokens)
                    {
                        if (token.Extent.StartOffset < position && position < token.Extent.EndOffset)
                        {
                            return(true);
                        }
                        if (position <= token.Extent.StartOffset)
                        {
                            return(false);
                        }
                    }
                    break;

                case EdgeTrackingMode.LeftEdgeIncluded:
                    foreach (var token in filteredTokens)
                    {
                        if (token.Extent.StartOffset <= position && position < token.Extent.EndOffset)
                        {
                            return(true);
                        }
                        if (position < token.Extent.StartOffset)
                        {
                            return(false);
                        }
                    }
                    break;

                case EdgeTrackingMode.RightEdgeIncluded:
                    foreach (var token in filteredTokens)
                    {
                        if (token.Extent.StartOffset < position && position <= token.Extent.EndOffset)
                        {
                            return(true);
                        }
                        if (position < token.Extent.StartOffset)
                        {
                            return(false);
                        }
                    }
                    break;

                case EdgeTrackingMode.BothEdgesIncluded:
                    foreach (var token in filteredTokens)
                    {
                        if (token.Extent.StartOffset <= position && position <= token.Extent.EndOffset)
                        {
                            return(true);
                        }
                        if (position < token.Extent.StartOffset)
                        {
                            return(false);
                        }
                    }
                    break;

                default:
                    break;
                }
            }

            return(false);
        }