Skip to content

Commit 4af33c3

Browse files
removed the IsExcel* and IsAccess* methods, and replaced with DetermineDocumentClassType()
1 parent f244a83 commit 4af33c3

File tree

2 files changed

+84
-178
lines changed

2 files changed

+84
-178
lines changed

Rubberduck.VBEEditor/ComManagement/TypeLibs/TypeLibs.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,55 @@ public bool MoveNext()
415415
}
416416
}
417417

418+
public enum DocClassType
419+
{
420+
Unrecognized,
421+
ExcelWorkbook,
422+
ExcelWorksheet,
423+
AccessForm,
424+
AccessReport,
425+
}
426+
427+
public struct KnownDocType
428+
{
429+
public string DocTypeInterfaceProgId;
430+
public DocClassType DocType;
431+
432+
public KnownDocType(string docTypeInterfaceProgId, DocClassType docType)
433+
{
434+
DocTypeInterfaceProgId = docTypeInterfaceProgId;
435+
DocType = docType;
436+
}
437+
}
438+
439+
public static class DocClassHelper
440+
{
441+
public static KnownDocType[] KnownDocumentInterfaces =
442+
{
443+
new KnownDocType("Excel._Workbook", DocClassType.ExcelWorkbook),
444+
new KnownDocType("Excel._Worksheet", DocClassType.ExcelWorksheet),
445+
new KnownDocType("Access._Form", DocClassType.AccessForm),
446+
new KnownDocType("Access._Form2", DocClassType.AccessForm),
447+
new KnownDocType("Access._Form3", DocClassType.AccessForm),
448+
new KnownDocType("Access._Report", DocClassType.AccessReport),
449+
new KnownDocType("Access._Report2", DocClassType.AccessReport),
450+
new KnownDocType("Access._Report3", DocClassType.AccessReport),
451+
};
452+
453+
// string array of the above progIDs, created once at runtime
454+
public static string[] KnownDocumentInterfaceProgIds;
455+
456+
static DocClassHelper()
457+
{
458+
int index = 0;
459+
KnownDocumentInterfaceProgIds = new string[KnownDocumentInterfaces.Length];
460+
foreach (var knownDocClass in KnownDocumentInterfaces)
461+
{
462+
KnownDocumentInterfaceProgIds[index++] = knownDocClass.DocTypeInterfaceProgId;
463+
}
464+
}
465+
}
466+
418467
// A wrapper for ITypeInfo provided by VBE, allowing safe managed consumption, plus adds StdModExecute functionality
419468
public class TypeInfoWrapper : ComTypes.ITypeInfo, IDisposable
420469
{
@@ -802,6 +851,15 @@ public TypeInfoWrapper GetSafeImplementedTypeInfo(int index)
802851
return GetSafeRefTypeInfo(href);
803852
}
804853

854+
public DocClassType DetermineDocumentClassType()
855+
{
856+
if (ImplementedInterfaces.DoesImplement(DocClassHelper.KnownDocumentInterfaceProgIds, out int matchId))
857+
{
858+
return DocClassHelper.KnownDocumentInterfaces[matchId].DocType;
859+
}
860+
return DocClassType.Unrecognized;
861+
}
862+
805863
public void Document(StringLineBuilder output, string qualifiedName, int implementsLevel)
806864
{
807865
output.AppendLine();

Rubberduck.VBEEditor/ComManagement/TypeLibs/TypeLibsAPI.cs

Lines changed: 26 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,8 @@ public string GetUserFormControlType(string projectName, string userFormName, st
3232
=> VBETypeLibsAPI.GetUserFormControlType(_ide, projectName, userFormName, controlName);
3333
public string GetDocumentClassControlType(string projectName, string documentClassName, string controlName)
3434
=> VBETypeLibsAPI.GetDocumentClassControlType(_ide, projectName, documentClassName, controlName);
35-
public bool IsExcelWorksheet(string projectName, string className)
36-
=> VBETypeLibsAPI.IsExcelWorksheet(_ide, projectName, className);
37-
public bool IsExcelWorkbook(string projectName, string className)
38-
=> VBETypeLibsAPI.IsExcelWorkbook(_ide, projectName, className);
39-
public bool IsAccessForm(string projectName, string className)
40-
=> VBETypeLibsAPI.IsAccessForm(_ide, projectName, className);
41-
public bool IsAccessReport(string projectName, string className)
42-
=> VBETypeLibsAPI.IsAccessReport(_ide, projectName, className);
35+
public DocClassType DetermineDocumentClassType(string projectName, string className)
36+
=> VBETypeLibsAPI.DetermineDocumentClassType(_ide, projectName, className);
4337
public string DocumentAll()
4438
=> VBETypeLibsAPI.DocumentAll(_ide);
4539
}
@@ -391,209 +385,63 @@ public static void SetProjectConditionalCompilationArgs(TypeLibWrapper projectTy
391385
}
392386

393387
/// <summary>
394-
/// Determines whether the specified document class is an Excel Workbook
388+
/// Determines whether the specified document class is a known document class type (e.g. Excel._Workbook, Access._Form)
395389
/// </summary>
396390
/// <param name="ide">Safe-com wrapper representing the VBE</param>
397391
/// <param name="projectName">VBA Project name, as declared in the VBE</param>
398-
/// <param name="className">Document class name, as declared in the VBA project</param>
399-
/// <returns>bool indicating whether the class is an Excel Workbook</returns>
400-
public static bool IsExcelWorkbook(IVBE ide, string projectName, string className)
401-
{
402-
using (var typeLibs = new VBETypeLibsAccessor(ide))
403-
{
404-
return IsExcelWorkbook(typeLibs.Get(projectName), className);
405-
}
406-
}
407-
408-
/// <summary>
409-
/// Determines whether the specified document class is an Excel Workbook
410-
/// </summary>
411-
/// <param name="project">Safe-com wrapper representing the VBA project</param>
412-
/// <param name="className">Document class name, as declared in the VBA project</param>
413-
/// <returns>bool indicating whether the class is an Excel Workbook</returns>
414-
public static bool IsExcelWorkbook(IVBProject project, string className)
415-
{
416-
using (var typeLib = TypeLibWrapper.FromVBProject(project))
417-
{
418-
return IsExcelWorkbook(typeLib, className);
419-
}
420-
}
421-
422-
/// <summary>
423-
/// Determines whether the specified document class is an Excel Workbook
424-
/// </summary>
425-
/// <param name="project">Safe-com wrapper representing the VBA component</param>
426-
/// <returns>bool indicating whether the class is an Excel Workbook</returns>
427-
public static bool IsExcelWorkbook(IVBComponent component)
428-
{
429-
return IsExcelWorkbook(component.ParentProject, component.Name);
430-
}
431-
432-
/// <summary>
433-
/// Determines whether the specified document class is an Excel Workbook
434-
/// </summary>
435-
/// <param name="projectTypeLib">Low-level ITypeLib wrapper representing the VBA project</param>
436-
/// <param name="className">Document class name, as declared in the VBA project</param>
437-
/// <returns>bool indicating whether the class is an Excel Workbook</returns>
438-
public static bool IsExcelWorkbook(TypeLibWrapper projectTypeLib, string className)
439-
{
440-
return DoesClassImplementInterface(projectTypeLib, className, "Excel._Workbook");
441-
}
442-
443-
/// <summary>
444-
/// Determines whether the specified document class is an Excel Worksheet
445-
/// </summary>
446-
/// <param name="ide">Safe-com wrapper representing the VBE</param>
447-
/// <param name="projectName">VBA Project name, as declared in the VBE</param>
448-
/// <param name="className">Document class name, as declared in the VBA project</param>
449-
/// <returns>bool indicating whether the class is an Excel Worksheet</returns>
450-
public static bool IsExcelWorksheet(IVBE ide, string projectName, string className)
451-
{
452-
using (var typeLibs = new VBETypeLibsAccessor(ide))
453-
{
454-
return IsExcelWorksheet(typeLibs.Get(projectName), className);
455-
}
456-
}
457-
458-
/// <summary>
459-
/// Determines whether the specified document class is an Excel Worksheet
460-
/// </summary>
461-
/// <param name="project">Safe-com wrapper representing the VBA project</param>
462-
/// <param name="className">Document class name, as declared in the VBA project</param>
463-
/// <returns>bool indicating whether the class is an Excel Worksheet</returns>
464-
public static bool IsExcelWorksheet(IVBProject project, string className)
465-
{
466-
using (var typeLib = TypeLibWrapper.FromVBProject(project))
467-
{
468-
return IsExcelWorksheet(typeLib, className);
469-
}
470-
}
471-
472-
/// <summary>
473-
/// Determines whether the specified document class is an Excel Worksheet
474-
/// </summary>
475-
/// <param name="project">Safe-com wrapper representing the VBA component</param>
476-
/// <returns>bool indicating whether the class is an Excel Worksheet</returns>
477-
public static bool IsExcelWorksheet(IVBComponent component)
478-
{
479-
return IsExcelWorksheet(component.ParentProject, component.Name);
480-
}
481-
482-
/// <summary>
483-
/// Determines whether the specified document class is an Excel Worksheet
484-
/// </summary>
485-
/// <param name="projectTypeLib">Low-level ITypeLib wrapper representing the VBA project</param>
486-
/// <param name="className">VBA Document class name, as declared in the VBA project</param>
487-
/// <returns>bool indicating whether the class is an Excel Worksheet</returns>
488-
public static bool IsExcelWorksheet(TypeLibWrapper projectTypeLib, string className)
489-
{
490-
return DoesClassImplementInterface(projectTypeLib, className, "Excel._Worksheet");
491-
}
492-
493-
/// <summary>
494-
/// Determines whether the specified document class is an Excel Workbook
495-
/// </summary>
496-
/// <param name="ide">Safe-com wrapper representing the VBE</param>
497-
/// <param name="projectName">VBA Project name, as declared in the VBE</param>
498-
/// <param name="className">Document class name, as declared in the VBA project</param>
499-
/// <returns>bool indicating whether the class is an Access Form</returns>
500-
public static bool IsAccessForm(IVBE ide, string projectName, string className)
392+
/// <param name="className">The name of the class document, as defined in the VBA project</param>
393+
/// <returns>DocClassType indicating the type of the document class module, or DocType.Unrecognized</returns>
394+
public static DocClassType DetermineDocumentClassType(IVBE ide, string projectName, string className)
501395
{
502396
using (var typeLibs = new VBETypeLibsAccessor(ide))
503397
{
504-
return IsAccessForm(typeLibs.Get(projectName), className);
505-
}
506-
}
507-
508-
/// <summary>
509-
/// Determines whether the specified document class is an Excel Workbook
510-
/// </summary>
511-
/// <param name="project">Safe-com wrapper representing the VBA project</param>
512-
/// <param name="className">Document class name, as declared in the VBA project</param>
513-
/// <returns>bool indicating whether the class is an Access Form</returns>
514-
public static bool IsAccessForm(IVBProject project, string className)
515-
{
516-
using (var typeLib = TypeLibWrapper.FromVBProject(project))
517-
{
518-
return IsAccessForm(typeLib, className);
398+
return DetermineDocumentClassType(typeLibs.Get(projectName), className);
519399
}
520400
}
521401

522402
/// <summary>
523-
/// Determines whether the specified document class is an Excel Workbook
524-
/// </summary>
525-
/// <param name="project">Safe-com wrapper representing the VBA component</param>
526-
/// <returns>bool indicating whether the class is an Access Form</returns>
527-
public static bool IsAccessForm(IVBComponent component)
528-
{
529-
return IsAccessForm(component.ParentProject, component.Name);
530-
}
531-
532-
/// <summary>
533-
/// Determines whether the specified document class is an Excel Workbook
403+
/// Determines whether the specified document class is a known document class type (e.g. Excel._Workbook, Access._Form)
534404
/// </summary>
535405
/// <param name="projectTypeLib">Low-level ITypeLib wrapper representing the VBA project</param>
536-
/// <param name="className">Document class name, as declared in the VBA project</param>
537-
/// <returns>bool indicating whether the class is an Access Form</returns>
538-
public static bool IsAccessForm(TypeLibWrapper projectTypeLib, string className)
406+
/// <param name="className">The name of the class document, as defined in the VBA project</param>
407+
/// <returns>DocClassType indicating the type of the document class module, or DocType.Unrecognized</returns>
408+
public static DocClassType DetermineDocumentClassType(TypeLibWrapper projectTypeLib, string className)
539409
{
540-
// The interface used by an Access form depends on the version of Access hosting the form
541-
// so we have to check for the current known interface prog-ids
542-
return DoesClassImplementInterface(projectTypeLib, className,
543-
new string[] { "Access._Form", "Access._Form2", "Access._Form3" }, out int matchIndex);
410+
return DetermineDocumentClassType(projectTypeLib.TypeInfos.Get(className));
544411
}
545412

546413
/// <summary>
547-
/// Determines whether the specified document class is an Excel Workbook
548-
/// </summary>
549-
/// <param name="ide">Safe-com wrapper representing the VBE</param>
550-
/// <param name="projectName">VBA Project name, as declared in the VBE</param>
551-
/// <param name="className">Document class name, as declared in the VBA project</param>
552-
/// <returns>bool indicating whether the class is an Access Form</returns>
553-
public static bool IsAccessReport(IVBE ide, string projectName, string className)
554-
{
555-
using (var typeLibs = new VBETypeLibsAccessor(ide))
556-
{
557-
return IsAccessReport(typeLibs.Get(projectName), className);
558-
}
559-
}
560-
561-
/// <summary>
562-
/// Determines whether the specified document class is an Excel Workbook
414+
/// Determines whether the specified document class is a known document class type (e.g. Excel._Workbook, Access._Form)
563415
/// </summary>
564416
/// <param name="project">Safe-com wrapper representing the VBA project</param>
565-
/// <param name="className">Document class name, as declared in the VBA project</param>
566-
/// <returns>bool indicating whether the class is an Access Form</returns>
567-
public static bool IsAccessReport(IVBProject project, string className)
417+
/// <param name="className">The name of the class document, as defined in the VBA project</param>
418+
/// <returns>DocClassType indicating the type of the document class module, or DocType.Unrecognized</returns>
419+
public static DocClassType DetermineDocumentClassType(IVBProject project, string className)
568420
{
569421
using (var typeLib = TypeLibWrapper.FromVBProject(project))
570422
{
571-
return IsAccessReport(typeLib, className);
423+
return DetermineDocumentClassType(typeLib.TypeInfos.Get(className));
572424
}
573425
}
574426

575427
/// <summary>
576-
/// Determines whether the specified document class is an Excel Workbook
428+
/// Determines whether the specified document class is a known document class type (e.g. Excel._Workbook, Access._Form)
577429
/// </summary>
578430
/// <param name="project">Safe-com wrapper representing the VBA component</param>
579-
/// <returns>bool indicating whether the class is an Access Form</returns>
580-
public static bool IsAccessReport(IVBComponent component)
431+
/// <returns>DocClassType indicating the type of the document class module, or DocType.Unrecognized</returns>
432+
public static DocClassType DetermineDocumentClassType(IVBComponent component)
581433
{
582-
return IsAccessReport(component.ParentProject, component.Name);
434+
return DetermineDocumentClassType(component.ParentProject, component.Name);
583435
}
584436

585437
/// <summary>
586-
/// Determines whether the specified document class is an Excel Workbook
438+
/// Determines whether the specified document class is a known document class type (e.g. Excel._Workbook, Access._Form)
587439
/// </summary>
588-
/// <param name="projectTypeLib">Low-level ITypeLib wrapper representing the VBA project</param>
589-
/// <param name="className">Document class name, as declared in the VBA project</param>
590-
/// <returns>bool indicating whether the class is an Access Form</returns>
591-
public static bool IsAccessReport(TypeLibWrapper projectTypeLib, string className)
440+
/// <param name="classTypeInfo">Low-level ITypeInfo wrapper representing the VBA project</param>
441+
/// <returns>DocClassType indicating the type of the document class module, or DocType.Unrecognized</returns>
442+
public static DocClassType DetermineDocumentClassType(TypeInfoWrapper classTypeInfo)
592443
{
593-
// The interface used by an Access form depends on the version of Access hosting the form
594-
// so we have to check for the current known interface prog-ids
595-
return DoesClassImplementInterface(projectTypeLib, className,
596-
new string[] { "Access._Report", "Access._Report2", "Access._Report3" }, out int matchIndex);
444+
return classTypeInfo.DetermineDocumentClassType();
597445
}
598446

599447
/// <summary>

0 commit comments

Comments
 (0)