protected override bool IsResultDeclaration(Declaration declaration, DeclarationFinder finder) { var module = declaration.QualifiedModuleName; var tree = _parseTreeProvider.GetParseTree(module, CodeKind.CodePaneCode); return(_emptyModuleVisitor.Visit(tree)); }
private (int index, bool isImplementsStatement) InsertionIndex(ExtractInterfaceModel model) { var tree = (ParserRuleContext)_parseTreeProvider.GetParseTree(model.TargetDeclaration.QualifiedModuleName, CodeKind.CodePaneCode); var moduleDeclarations = tree.GetDescendent <VBAParser.ModuleDeclarationsContext>(); if (moduleDeclarations == null) { return(-1, false); } var lastImplementsStatement = moduleDeclarations .GetDescendents <VBAParser.ImplementsStmtContext>() .LastOrDefault(); if (lastImplementsStatement != null) { return(lastImplementsStatement.Stop.TokenIndex, true); } var lastOptionStatement = moduleDeclarations .GetDescendents <VBAParser.ModuleOptionContext>() .LastOrDefault(); if (lastOptionStatement != null) { return(lastOptionStatement.Stop.TokenIndex, false); } return(-1, false); }
private void InsertAfterLastModuleAttribute(IModuleRewriter rewriter, QualifiedModuleName module, string codeToAdd) { var moduleParseTree = (ParserRuleContext)_parseTreeProvider.GetParseTree(module, CodeKind.AttributesCode); var lastModuleAttribute = moduleParseTree.GetDescendents <VBAParser.ModuleAttributesContext>() .Where(moduleAttributes => moduleAttributes.attributeStmt() != null) .SelectMany(moduleAttributes => moduleAttributes.attributeStmt()) .OrderBy(moduleAttribute => moduleAttribute.stop.TokenIndex) .LastOrDefault(); if (lastModuleAttribute == null) { //This should never happen for a real module. var codeToInsert = codeToAdd + Environment.NewLine; rewriter.InsertBefore(0, codeToInsert); } else { var codeToInsert = Environment.NewLine + codeToAdd; rewriter.InsertAfter(lastModuleAttribute.stop.TokenIndex, codeToInsert); } }
public void AddAttribute(IRewriteSession rewriteSession, Declaration declaration, string attribute, IReadOnlyList <string> values) { if (string.IsNullOrEmpty(attribute)) { return; } //Attributes must have at least one value. if (values == null || !values.Any()) { return; } if (declaration == null) { _logger.Warn("Tried to add an attribute to a declaration that is null."); _logger.Trace($"Tried to add attribute {attribute} with values {AttributeValuesText(values)} to a declaration that is null."); return; } if (rewriteSession.TargetCodeKind != CodeKind.AttributesCode) { _logger.Warn($"Tried to add an attribute with a rewriter not suitable for attributes. (target code kind = {rewriteSession.TargetCodeKind})"); _logger.Trace($"Tried to add attribute {attribute} with values {AttributeValuesText(values)} to {declaration.QualifiedModuleName} using a rewriter not suitable for attributes."); return; } //VB_Ext_Key is special in that this attribute can be declared multiple times, but only once for each key. if (attribute.ToUpperInvariant().EndsWith("VB_EXT_KEY")) { if (values.Count != 2) { return; } //Is the key already defined as external key? if (declaration.Attributes.Any(attrbt => attrbt.Name.Equals(attribute, StringComparison.OrdinalIgnoreCase) && attrbt.Values[0].Equals(values[0]))) { return; } } else if (declaration.Attributes.HasAttribute(attribute)) { return; } var rewriter = rewriteSession.CheckOutModuleRewriter(declaration.QualifiedModuleName); if (declaration.DeclarationType.HasFlag(DeclarationType.Module)) { var moduleParseTree = (ParserRuleContext)_parseTreeProvider.GetParseTree(declaration.QualifiedModuleName, CodeKind.AttributesCode); var lastModuleAttribute = moduleParseTree.GetDescendents <VBAParser.ModuleAttributesContext>() .Where(moduleAttributes => moduleAttributes.attributeStmt() != null) .SelectMany(moduleAttributes => moduleAttributes.attributeStmt()) .OrderBy(moduleAttribute => moduleAttribute.stop.TokenIndex) .LastOrDefault(); if (lastModuleAttribute == null) { //This should never happen for a real module. var codeToInsert = $"Attribute {attribute} = {AttributeValuesText(values)}{Environment.NewLine}"; rewriter.InsertBefore(0, codeToInsert); } else { var codeToInsert = $"{Environment.NewLine}Attribute {attribute} = {AttributeValuesText(values)}"; rewriter.InsertAfter(lastModuleAttribute.stop.TokenIndex, codeToInsert); } } else { var attributesContext = declaration.AttributesPassContext; var firstEndOfLineInMember = attributesContext.GetDescendent <VBAParser.EndOfLineContext>(); if (firstEndOfLineInMember == null) { var codeToInsert = $"{Environment.NewLine}Attribute {attribute} = {AttributeValuesText(values)}"; rewriter.InsertAfter(declaration.AttributesPassContext.Stop.TokenIndex, codeToInsert); } else { var codeToInsert = $"Attribute {attribute} = {AttributeValuesText(values)}{Environment.NewLine}"; rewriter.InsertAfter(firstEndOfLineInMember.Stop.TokenIndex, codeToInsert); } } }