Exemple #1
0
        private bool ExecuteActionInCppContextOnly(IActionContext actionContext, Func <IActionContext, bool> cppAction)
        {
            if (actionContext.EnsureWritable() != EnsureWritableResult.SUCCESS)
            {
                return(false);
            }

            if (!FindCgContent(actionContext.TextControl, out var lexer))
            {
                return(false);
            }

            if (lexer.TokenType is CppTokenNodeType)
            {
                return(cppAction(actionContext));
            }

            return(false);
        }
        private bool HandleBackspaceAction(IActionContext actionContext)
        {
            if (actionContext.EnsureWritable() != EnsureWritableResult.SUCCESS)
            {
                return(false);
            }

            if (ShouldIgnoreCaretPosition(actionContext.TextControl, out var _))
            {
                return(false);
            }

            // HLSL could not be affected by smart backspace action, performance optimization
            using (new ShaderLabDoNotFormatInjectionsCookie())
            {
                if (HandleUnindentOnBackspace(actionContext))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #3
0
        private bool WrapAction(IActionContext actionContext, Func <IActionContext, bool> cppAction, Func <IActionContext, CachingLexer, bool> shaderLabAction = null)
        {
            if (actionContext.EnsureWritable() != EnsureWritableResult.SUCCESS)
            {
                return(false);
            }

            if (!FindCgContent(actionContext.TextControl, out var lexer))
            {
                return(false);
            }

            if (lexer.TokenType is CppTokenNodeType)
            {
                return(cppAction(actionContext));
            }

            if (shaderLabAction != null)
            {
                return(shaderLabAction(actionContext, lexer));
            }

            return(false);
        }
        private bool HandleEnterAction(IActionContext actionContext)
        {
            if (actionContext.EnsureWritable() != EnsureWritableResult.SUCCESS)
            {
                return(false);
            }
            if (ShouldIgnoreCaretPosition(actionContext.TextControl, out var cachingLexer))
            {
                return(false);
            }

            var textControl = actionContext.TextControl;

            if (GetTypingAssistOption(textControl, TypingAssistOptions.SmartIndentOnEnterExpression))
            {
                using (CommandProcessor.UsingCommand("Smart Enter"))
                {
                    if (textControl.Selection.OneDocRangeWithCaret().Length > 0)
                    {
                        return(false);
                    }

                    var caret = textControl.Caret.Offset();
                    if (caret == 0)
                    {
                        return(false);
                    }

                    var closedCount = 0;
                    cachingLexer.FindTokenAt(caret - 1);
                    var tt = cachingLexer.TokenType;
                    while (tt != null)
                    {
                        if (tt == ShaderLabTokenType.RBRACE)
                        {
                            closedCount++;
                        }
                        if (tt == ShaderLabTokenType.LBRACE)
                        {
                            if (closedCount == 0)
                            {
                                var line       = textControl.Document.GetCoordsByOffset(cachingLexer.TokenStart).Line;
                                var lineOffset = textControl.Document.GetLineStartOffset(line);

                                if (cachingLexer.FindTokenAt(lineOffset))
                                {
                                    // e.g
                                    //Shader "Custom/Test2_hlsl" {
                                    //<caret>    Properties {

                                    //<caret>{

                                    //<caret>    {
                                    var    baseIndent = myInjectedHlslDummyFormatter.GetFormatSettingsService(textControl).GetIndentStr();
                                    string indent;
                                    if (cachingLexer.TokenType == ShaderLabTokenType.WHITESPACE)
                                    {
                                        indent = cachingLexer.GetTokenText() + baseIndent;
                                    }
                                    else
                                    {
                                        indent = baseIndent;
                                    }

                                    textControl.Document.InsertText(caret, "\n" + indent);
                                    return(true);
                                }
                            }

                            closedCount--;
                        }

                        cachingLexer.Advance(-1);
                        tt = cachingLexer.TokenType;
                    }
                }
            }

            return(false);
        }