Exemplo n.º 1
0
        void OnUpdateToggleComment(CommandInfo info)
        {
            var scope = Editor.SyntaxHighlighting.GetScopeStackAsync(Editor.CaretOffset, CancellationToken.None).WaitAndGetResult(CancellationToken.None);
            var lang  = TextMateLanguage.Create(scope);

            info.Visible = lang.LineComments.Count + lang.BlockComments.Count > 0;
        }
Exemplo n.º 2
0
        static int GetLastSourceCodePosition(IReadonlyTextDocument document, int lineOffset)
        {
            var  line             = document.GetLineByOffset(lineOffset);
            bool isInBlockComment = false;
            bool isInLineComment  = false;
            int  curStringQuote   = -1;

            var lang               = TextMateLanguage.Create(SyntaxHighlightingService.GetScopeForFileName(document.FileName));
            var lineComments       = lang.LineComments.ToArray();
            var blockCommentStarts = lang.BlockComments.Select(b => b.Item1).ToList();
            var blockCommentEnds   = lang.BlockComments.Select(b => b.Item2).ToList();

            var stringQuotes = new string [] { "\"", "'" };

            for (int i = 0; i < line.Length; i++)
            {
                int offset = line.Offset + i;
                // check line comments
                if (!isInBlockComment && curStringQuote < 0)
                {
                    isInLineComment = StartsWithListMember(document, lineComments, offset) >= 0;
                    if (isInLineComment)
                    {
                        return(System.Math.Min(offset, lineOffset));
                    }
                }
                // check block comments
                if (!isInLineComment && curStringQuote < 0)
                {
                    if (!isInBlockComment)
                    {
                        isInBlockComment = StartsWithListMember(document, blockCommentStarts, offset) >= 0;
                    }
                    else
                    {
                        isInBlockComment = StartsWithListMember(document, blockCommentEnds, offset) < 0;
                    }
                }

                if (!isInBlockComment && !isInLineComment)
                {
                    int j = StartsWithListMember(document, stringQuotes, offset);
                    if (j >= 0)
                    {
                        if (curStringQuote >= 0)
                        {
                            if (curStringQuote == j)
                            {
                                curStringQuote = -1;
                            }
                        }
                        else
                        {
                            curStringQuote = j;
                        }
                    }
                }
            }
            return(lineOffset);
        }
Exemplo n.º 3
0
        async void ToggleCodeCommentWithBlockComments()
        {
            var scope = await Editor.SyntaxHighlighting.GetScopeStackAsync(Editor.CaretOffset, CancellationToken.None);

            var lang         = TextMateLanguage.Create(scope);
            var lineComments = lang.LineComments.ToArray();
            var blockStarts  = lang.BlockComments.Select(b => b.Item1).ToList();
            var blockEnds    = lang.BlockComments.Select(b => b.Item2).ToList();

            if (blockStarts.Count == 0 || blockEnds.Count == 0)
            {
                return;
            }

            string blockStart = blockStarts[0];
            string blockEnd   = blockEnds[0];

            using (var undo = Editor.OpenUndoGroup()) {
                IDocumentLine startLine;
                IDocumentLine endLine;

                if (Editor.IsSomethingSelected)
                {
                    startLine = Editor.GetLineByOffset(Editor.SelectionRange.Offset);
                    endLine   = Editor.GetLineByOffset(Editor.SelectionRange.EndOffset);

                    // If selection ends at begining of line... This is visible as previous line
                    // is selected, hence we want to select previous line Bug 26287
                    if (endLine.Offset == Editor.SelectionRange.EndOffset)
                    {
                        endLine = endLine.PreviousLine;
                    }
                }
                else
                {
                    startLine = endLine = Editor.GetLine(Editor.CaretLine);
                }
                string startLineText = Editor.GetTextAt(startLine.Offset, startLine.Length);
                string endLineText   = Editor.GetTextAt(endLine.Offset, endLine.Length);
                if (startLineText.StartsWith(blockStart, StringComparison.Ordinal) && endLineText.EndsWith(blockEnd, StringComparison.Ordinal))
                {
                    Editor.RemoveText(endLine.Offset + endLine.Length - blockEnd.Length, blockEnd.Length);
                    Editor.RemoveText(startLine.Offset, blockStart.Length);
                    if (Editor.IsSomethingSelected)
                    {
                        Editor.SelectionAnchorOffset -= blockEnd.Length;
                    }
                }
                else
                {
                    Editor.InsertText(endLine.Offset + endLine.Length, blockEnd);
                    Editor.InsertText(startLine.Offset, blockStart);
                    if (Editor.IsSomethingSelected)
                    {
                        Editor.SelectionAnchorOffset += blockEnd.Length;
                    }
                }
            }
        }
Exemplo n.º 4
0
        bool TryGetLineCommentTag(out string commentTag)
        {
            var scope = Editor.SyntaxHighlighting.GetScopeStackAsync(Editor.CaretOffset, CancellationToken.None).WaitAndGetResult(CancellationToken.None);
            var lang  = TextMateLanguage.Create(scope);

            if (lang.LineComments.Count == 0)
            {
                commentTag = null;
                return(false);
            }
            commentTag = lang.LineComments [0];
            return(true);
        }
Exemplo n.º 5
0
        public static string[] GetCommentTags(string fileName)
        {
            //Document doc = IdeApp.Workbench.ActiveDocument;
            var lang = TextMateLanguage.Create(SyntaxHighlightingService.GetScopeForFileName(fileName));

            if (lang.LineComments.Count > 0)
            {
                return(lang.LineComments.ToArray());
            }

            if (lang.BlockComments.Count > 0)
            {
                return new [] { lang.BlockComments[0].Item1, lang.BlockComments[0].Item2 }
            }
            ;
            return(null);
        }
Exemplo n.º 6
0
        public static async Task <IEnumerable <CodeTemplate> > GetCodeTemplatesAsync(TextEditor editor, CancellationToken cancellationToken = default(CancellationToken))
        {
            var result = new List <CodeTemplate> ();

            result.AddRange(GetCodeTemplates(editor.MimeType));
            var scope = await editor.SyntaxHighlighting.GetScopeStackAsync(editor.CaretOffset, cancellationToken);

            foreach (var setting in TextMateLanguage.Create(scope).Snippets)
            {
                var convertedTemplate = ConvertToTemplate(setting);
                if (convertedTemplate != null)
                {
                    result.Add(convertedTemplate);
                }
            }
            return(result);
        }
Exemplo n.º 7
0
        static bool StartsInLineComment(IReadonlyTextDocument document, int offset)
        {
            var lang               = TextMateLanguage.Create(SyntaxHighlightingService.GetScopeForFileName(document.FileName));
            var lineComments       = lang.LineComments.ToArray();
            var blockCommentStarts = lang.BlockComments.Select(b => b.Item1).ToList();
            var blockCommentEnds   = lang.BlockComments.Select(b => b.Item2).ToList();

            var line = document.GetLineByOffset(offset);

            for (int i = line.Offset; i < offset; i++)
            {
                if (StartsWithListMember(document, lineComments, i) >= 0)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 8
0
        static int SearchMatchingBracketForward(IReadonlyTextDocument document, int offset, char openBracket, char closingBracket, CancellationToken cancellationToken)
        {
            bool isInBlockComment = false;
            bool isInLineComment  = false;
            int  curStringQuote   = -1;

            bool startsInLineComment = StartsInLineComment(document, offset);

            var lang               = TextMateLanguage.Create(SyntaxHighlightingService.GetScopeForFileName(document.FileName));
            var lineComments       = lang.LineComments.ToArray();
            var blockCommentStarts = lang.BlockComments.Select(b => b.Item1).ToList();
            var blockCommentEnds   = lang.BlockComments.Select(b => b.Item2).ToList();

            var stringQuotes = new string [] { "\"", "'" };
            int depth        = -1;

            while (offset >= 0 && offset < document.Length)
            {
                if (offset % 100 == 0 && cancellationToken.IsCancellationRequested)
                {
                    return(-1);
                }
                if (curStringQuote < 0)
                {
                    // check line comments
                    if (!isInBlockComment && !isInLineComment)
                    {
                        isInLineComment = StartsWithListMember(document, lineComments, offset) >= 0;
                    }

                    // check block comments
                    if (!isInLineComment)
                    {
                        if (!isInBlockComment)
                        {
                            isInBlockComment = StartsWithListMember(document, blockCommentStarts, offset) >= 0;
                        }
                        else
                        {
                            isInBlockComment = StartsWithListMember(document, blockCommentEnds, offset) < 0;
                        }
                    }
                }

                if (!isInBlockComment && !isInLineComment)
                {
                    int i = StartsWithListMember(document, stringQuotes, offset);
                    if (i >= 0)
                    {
                        if (curStringQuote >= 0)
                        {
                            if (curStringQuote == i)
                            {
                                curStringQuote = -1;
                            }
                        }
                        else
                        {
                            curStringQuote = i;
                        }
                    }
                }

                char ch = document.GetCharAt(offset);
                switch (ch)
                {
                case '\n':
                case '\r':
                    if (startsInLineComment)
                    {
                        return(-1);
                    }
                    isInLineComment = false;
                    break;

                default:
                    if (ch == closingBracket)
                    {
                        if (!(isInLineComment || curStringQuote >= 0 || isInBlockComment))
                        {
                            --depth;
                        }
                    }
                    else if (ch == openBracket)
                    {
                        if (!(isInLineComment || curStringQuote >= 0 || isInBlockComment))
                        {
                            ++depth;
                            if (depth == 0)
                            {
                                return(offset);
                            }
                        }
                    }
                    break;
                }
                offset++;
            }
            return(-1);
        }