Пример #1
0
 public string Get(
     string shortcut,
     SymbolDefinition context,
     FGGrammar.TokenSet expectedTokens,
     Scope scope)
 {
     return(null);
 }
Пример #2
0
 public static IEnumerable <string> GetShortcuts(SymbolDefinition context, FGGrammar.TokenSet expected)
 {
     foreach (var snippet in snippets)
     {
         var text = snippet.Value;
         if (IsValid(ref text, context, expected))
         {
             yield return(snippet.Key);
         }
     }
 }
Пример #3
0
        public static string Get(string shortcut, SymbolDefinition context, FGGrammar.TokenSet expected)
        {
            string text;

            if (!snippets.TryGetValue(shortcut, out text))
            {
                return(null);
            }

            if (!IsValid(ref text, context, expected))
            {
                return(null);
            }

            return(text);
        }
Пример #4
0
        public IEnumerable <SnippetCompletion> EnumSnippets(
            SymbolDefinition context,
            FGGrammar.TokenSet expectedTokens,
            SyntaxToken tokenLeft,
            Scope scope)
        {
            if (tokenLeft == null || tokenLeft.parent == null || tokenLeft.parent.parent == null)
            {
                yield break;
            }

            if (tokenLeft.tokenKind != SyntaxToken.Kind.Punctuator)
            {
                yield break;
            }

            if (tokenLeft.text != "{" && tokenLeft.text != "}" && tokenLeft.text != ";")
            {
                yield break;
            }

            var bodyScope = scope as BodyScope;

            if (bodyScope == null)
            {
                yield break;
            }

            contextType = bodyScope.definition as TypeDefinitionBase;
            if (contextType == null || contextType.kind != SymbolKind.Class)
            {
                yield break;
            }

            List <SnippetCompletion> magicMethods;

            if (contextType.DerivesFrom(monoBehaviourType))
            {
                magicMethods = monoBehaviourMagicMethods;
            }
            else if (contextType.DerivesFrom(scriptableWizardType))
            {
                magicMethods = scriptableWizardMagicMethods;
            }
            else if (contextType.DerivesFrom(editorWindowType))
            {
                magicMethods = editorWindowMagicMethods;
            }
            else if (contextType.DerivesFrom(editorType))
            {
                magicMethods = editorMagicMethods;
            }
            else if (contextType.DerivesFrom(assetPostprocessorType))
            {
                magicMethods = assetPostprocessorMagicMethods;
            }
            else if (contextType.DerivesFrom(scriptableObjectType))
            {
                magicMethods = scriptableObjectMagicMethods;
            }
            else
            {
                yield break;
            }

            var baseType = contextType.BaseType();

            var tempLeaf = new ParseTree.Leaf()
            {
                token = new SyntaxToken(SyntaxToken.Kind.Identifier, "")
            };

            foreach (var magic in magicMethods)
            {
                ((IMagicMethod)magic).BaseSymbol = null;

                if (contextType.FindName(magic.name, -1, false) != null)
                {
                    continue;
                }

                tempLeaf.token.text = magic.name;
                baseType.ResolveMember(tempLeaf, scope, -1, false);
                var baseSymbol = tempLeaf.resolvedSymbol;
                if (baseSymbol == null || baseSymbol.kind == SymbolKind.Error)
                {
                    yield return(magic);

                    continue;
                }

                var asMethodGroup = baseSymbol as MethodGroupDefinition;
                if (baseSymbol.kind != SymbolKind.MethodGroup || asMethodGroup == null)
                {
                    if (!baseSymbol.IsPrivate)
                    {
                        ((IMagicMethod)magic).BaseSymbol = baseSymbol;
                    }
                    yield return(magic);

                    continue;
                }

                bool yield          = true;
                var  magicSignature = ((IMagicMethod)magic).GetParametersString();
                foreach (var baseMethod in asMethodGroup.methods)
                {
                    if (baseMethod.PrintParameters(baseMethod.GetParameters(), true) == magicSignature)
                    {
                        if (baseMethod.IsOverride || baseMethod.IsVirtual || baseMethod.IsAbstract)
                        {
                            yield = false;
                            break;
                        }

                        if (!baseMethod.IsPrivate)
                        {
                            var returnType = baseMethod.ReturnType();
                            if (returnType == null || returnType.kind == SymbolKind.Error)
                            {
                                ((IMagicMethod)magic).BaseSymbol = asMethodGroup;
                            }
                            else
                            {
                                var baseIsCoroutine = returnType.name == "IEnumerator";
                                var returnsVoid     = baseMethod.ReturnType() == SymbolDefinition.builtInTypes_void;
                                if (!baseIsCoroutine && !returnsVoid)
                                {
                                    ((IMagicMethod)magic).BaseSymbol = asMethodGroup;
                                }
                                else
                                {
                                    ((IMagicMethod)magic).BaseSymbol = baseMethod;
                                }
                            }
                        }
                        break;
                    }
                }

                if (yield)
                {
                    yield return(magic);
                }
            }
        }
Пример #5
0
        public IEnumerable <SnippetCompletion> EnumSnippets(
            SymbolDefinition context,
            FGGrammar.TokenSet expectedTokens,
            SyntaxToken tokenLeft,
            Scope scope)
        {
            OverrideMethod.context = scope;

            if (tokenLeft == null || tokenLeft.parent == null || tokenLeft.parent.parent == null)
            {
                yield break;
            }

            if (tokenLeft.tokenKind != SyntaxToken.Kind.Keyword)
            {
                yield break;
            }

            if (tokenLeft.text != "override")
            {
                yield break;
            }

            var bodyScope = scope as BodyScope;

            if (bodyScope == null)
            {
                yield break;
            }

            var contextType = bodyScope.definition as TypeDefinitionBase;

            if (contextType == null || contextType.kind != SymbolKind.Class && contextType.kind != SymbolKind.Struct)
            {
                yield break;
            }

            var baseType = contextType.BaseType();

            if (baseType == null || baseType.kind != SymbolKind.Class && baseType.kind != SymbolKind.Struct)
            {
                yield break;
            }

            var overrideMethodCandidates = new List <MethodDefinition>();

            baseType.ListOverrideCandidates(overrideMethodCandidates, contextType.Assembly);
            if (overrideMethodCandidates.Count == 0)
            {
                yield break;
            }

            var textBuffer = FGTextBuffer.activeEditor.TextBuffer;
            var firstToken = tokenLeft.parent.parent.GetFirstLeaf().token;

            if (firstToken.formatedLine != tokenLeft.formatedLine)
            {
                firstToken = tokenLeft.formatedLine.tokens[0];
                while (firstToken.tokenKind <= SyntaxToken.Kind.LastWSToken)
                {
                    firstToken = firstToken.formatedLine.tokens[firstToken.TokenIndex + 1];
                }
            }
            var tokenSpan = textBuffer.GetTokenSpan(firstToken.parent);

            OverrideMethod.overrideTextLength = FGTextBuffer.activeEditor.caretPosition.characterIndex - tokenSpan.StartPosition.index;

            foreach (var method in overrideMethodCandidates)
            {
                var methodGroup = contextType.FindName(method.name, -1, false) as MethodGroupDefinition;
                if (methodGroup != null)
                {
                    bool skipThis  = false;
                    var  signature = method.PrintParameters(method.GetParameters(), true);
                    foreach (var m in methodGroup.methods)
                    {
                        if (method.NumTypeParameters == m.NumTypeParameters &&
                            signature == m.PrintParameters(m.GetParameters()))
                        {
                            skipThis = true;
                            break;
                        }
                    }
                    if (skipThis)
                    {
                        continue;
                    }
                }

                var overrideCompletion = new OverrideMethod(method);
                yield return(overrideCompletion);
            }
        }
        public IEnumerable <SnippetCompletion> EnumSnippets(
            SymbolDefinition context,
            FGGrammar.TokenSet expectedTokens,
            SyntaxToken tokenLeft,
            Scope scope)
        {
            if (scope == null)
            {
                yield break;
            }

            var bodyScope = scope as BodyScope ?? scope.parentScope as BodyScope;

            if (bodyScope == null)
            {
                yield break;
            }

            var contextType = bodyScope.definition as TypeDefinitionBase;

            if (contextType == null || contextType.kind != SymbolKind.Class && contextType.kind != SymbolKind.Struct)
            {
                yield break;
            }

            context = contextType;

            if (tokenLeft == null || tokenLeft.parent == null || tokenLeft.parent.parent == null)
            {
                yield break;
            }

            if (tokenLeft.tokenKind != SyntaxToken.Kind.Punctuator)
            {
                yield break;
            }

            if (tokenLeft.text != "{" && tokenLeft.text != "}" && tokenLeft.text != ";")
            {
                yield break;
            }

            for (var i = contextType.members.Count; i-- > 0;)
            {
                var member = contextType.members[i];
                if (member.kind == SymbolKind.Field)
                {
                    var type = member.TypeOf();
                    if (type == null || type.kind == SymbolKind.Error || !(type is TypeDefinitionBase))
                    {
                        continue;
                    }

                    var fieldName = member.name;
                    if (string.IsNullOrEmpty(fieldName) || fieldName[0] == '.')
                    {
                        continue;
                    }

                    fieldName = char.ToUpperInvariant(fieldName[0]) + fieldName.Substring(1);
                    var propertyName = fieldName;
                    for (var suffix = 1; contextType.members.Contains(propertyName, -1); ++suffix)
                    {
                        propertyName = fieldName + suffix.ToString();
                    }

                    yield return(new MyCompletion(propertyName, member, true));           // "{0} {{ get {{...}} set {{...}} }}");

                    yield return(new MyCompletion(propertyName, member, false));          // "{0} {{ get {{...}} }}");
                }
            }
        }
Пример #7
0
        public static bool IsValid(ref string expanded, SymbolDefinition context, FGGrammar.TokenSet expected)
        {
            var methodDef    = context as MethodDefinition;
            var checkKeyword = false;

            for (var index = 0; (index = expanded.IndexOf('$', index)) != -1;)
            {
                var end = expanded.IndexOf('$', index + 1);
                if (end < index)
                {
                    break;
                }

                var macro = expanded.Substring(index + 1, end - index - 1);
                switch (macro)
                {
                case "MethodName":
                case "ArgumentList":
                    if (methodDef == null)
                    {
                        return(false);
                    }
                    break;

                case "ValidIfKeywordExpected":
                    checkKeyword = true;
                    expanded     = expanded.Remove(index, end - index + 2);
                    continue;

                case "ValidAsStatement":
                    if (expected != null && !expected.Matches(CsGrammar.Instance.tokenStatement))
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;

                case "NotValidAsStatement":
                    if (expected != null && expected.Matches(CsGrammar.Instance.tokenStatement))
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;

                case "ValidAsClassMember":
                    if (expected != null && !expected.Matches(CsGrammar.Instance.tokenClassBody))
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;

                case "ValidAsStructMember":
                    if (expected != null && !expected.Matches(CsGrammar.Instance.tokenStructBody))
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;

                case "ValidAsInterfaceMember":
                    if (expected != null && !expected.Matches(CsGrammar.Instance.tokenInterfaceBody))
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;

                case "ValidAsNamespaceMember":
                    if (expected != null && !expected.Matches(CsGrammar.Instance.tokenNamespaceBody))
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;

                case "ValidAsTypeDeclaration":
                    if (expected != null && !(
                            expected.Matches(CsGrammar.Instance.tokenNamespaceBody) ||
                            expected.Matches(CsGrammar.Instance.tokenClassBody) ||
                            expected.Matches(CsGrammar.Instance.tokenStructBody)
                            ))
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;

                case "ValidInMethodBody":
                    if (methodDef == null)
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;

                case "ValidInPropertyBody":
                    if (context == null ||
                        context.kind != SymbolKind.Property &&
                        (context.parentSymbol == null || context.parentSymbol.kind != SymbolKind.Property))
                    {
                        return(false);
                    }
                    expanded = expanded.Remove(index, end - index + 2);
                    continue;
                }
                index = end + 1;
            }

            if (checkKeyword && expected != null)
            {
                var index = 0;
                while (expanded[index] >= 'a' && expanded[index] <= 'z')
                {
                    ++index;
                }
                if (index == 0)
                {
                    if (expanded.StartsWith("!=", System.StringComparison.Ordinal) || expanded.StartsWith("==", System.StringComparison.Ordinal))
                    {
                        index = 2;
                    }
                    else
                    {
                        return(false);
                    }
                }
                var keyword = expanded.Substring(0, index);
                var tokenId = CsGrammar.Instance.TokenToId(keyword);
                if (!expected.Matches(tokenId))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #8
0
        public static IEnumerable <SnippetCompletion> EnumSnippets(SymbolDefinition context, FGGrammar.TokenSet expected, SyntaxToken tokenLeft, Scope scope)
        {
            foreach (var snippet in snippets)
            {
                var text = snippet.Value;
                if (IsValid(ref text, context, expected))
                {
                    yield return(new SnippetCompletion(snippet.Key + "..."));
                }
            }

            if (snippetsProviders == null)
            {
                snippetsProviders = new List <ISnippetProvider>();
                var types = typeof(CodeSnippets).Assembly.GetTypes();
                foreach (var type in types)
                {
                    if (typeof(ISnippetProvider).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract)
                    {
                        try
                        {
                            var instance = System.Activator.CreateInstance(type) as ISnippetProvider;
                            snippetsProviders.Add(instance);
                        }
                        catch (System.Exception e)
                        {
                            Debug.LogException(e);
                        }
                    }
                }
            }

            foreach (var snippetsProvider in snippetsProviders)
            {
                foreach (var snippet in snippetsProvider.EnumSnippets(context, expected, tokenLeft, scope))
                {
                    yield return(snippet);
                }
            }
        }