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; } }
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; }
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 {{...}} }}"); } } }
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 string Get( string shortcut, SymbolDefinition context, FGGrammar.TokenSet expectedTokens, Scope scope) { return null; }
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; }
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; } }