Esempio n. 1
0
        protected override (bool isResult, string properties) IsResultReferenceWithAdditionalProperties(IdentifierReference reference, DeclarationFinder finder)
        {
            if (reference.IdentifierName.Equals(Tokens.Me, System.StringComparison.InvariantCultureIgnoreCase))
            {
                // if Me is a worksheet module,
                return(false, null);
            }

            var hostWorkbookDeclaration = GetHostWorkbookDeclaration(finder);

            var context = reference.Context as VBAParser.MemberAccessExprContext
                          ?? reference.Context.Parent as VBAParser.MemberAccessExprContext
                          ?? reference.Context.Parent.Parent as VBAParser.MemberAccessExprContext;

            if (context is VBAParser.MemberAccessExprContext memberAccess)
            {
                var appObjectDeclaration   = GetHostApplicationDeclaration(finder);
                var isApplicationQualifier = appObjectDeclaration.References.Any(appRef =>
                                                                                 context.GetSelection().Contains(appRef.Selection) &&
                                                                                 appRef.QualifiedModuleName.Equals(reference.QualifiedModuleName));

                if (isApplicationQualifier)
                {
                    // Application.Sheets(...) is referring to the ActiveWorkbook, not necessarily ThisWorkbook.
                    return(false, null);
                }
            }

            var isHostWorkbookQualifier = hostWorkbookDeclaration.References.Any(thisWorkbookRef =>
                                                                                 context.GetSelection().Contains(thisWorkbookRef.Selection) &&
                                                                                 thisWorkbookRef.QualifiedModuleName.Equals(reference.QualifiedModuleName));

            var parentModule = finder.ModuleDeclaration(reference.QualifiedModuleName);

            if (!isHostWorkbookQualifier && parentModule is ProceduralModuleDeclaration)
            {
                // in a standard module the reference is against ActiveWorkbook unless it's explicitly against ThisWorkbook.
                return(false, null);
            }

            var sheetNameArgumentLiteralExpressionContext = SheetNameArgumentLiteralExpressionContext(reference);

            if (sheetNameArgumentLiteralExpressionContext?.STRINGLITERAL() == null)
            {
                return(false, null);
            }

            var projectId = reference.QualifiedModuleName.ProjectId;
            var sheetName = sheetNameArgumentLiteralExpressionContext.GetText().FromVbaStringLiteral();
            var codeName  = CodeNameOfVBComponentMatchingSheetName(projectId, sheetName);

            if (codeName == null)
            {
                return(false, null);
            }

            return(true, codeName);
        }
Esempio n. 2
0
        private static ClassModuleDeclaration GetHostWorkbookDeclaration(DeclarationFinder finder)
        {
            var documentModuleQMNs        = finder.AllModules.Where(m => m.ComponentType == ComponentType.Document);
            ClassModuleDeclaration result = null;

            foreach (var qmn in documentModuleQMNs)
            {
                var declaration = finder.ModuleDeclaration(qmn) as ClassModuleDeclaration;
                if (declaration.Supertypes.Any(t => t.IdentifierName.Equals("Workbook") && t.ProjectName == "Excel" && !t.IsUserDefined))
                {
                    result = declaration;
                    break;
                }
            }

            return(result ?? throw new System.InvalidOperationException("Failed to find the host Workbook declaration."));
        }