Skip to content

Commit

Permalink
Resolve NRE when the active file is null
Browse files Browse the repository at this point in the history
  • Loading branch information
Impertatore committed Dec 19, 2022
1 parent 7f6babc commit 0f2f8b4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ private void EditorController_Opening(object sender, CancelDocumentEventArgs e)

var project = e.Document.Project;
var projectInfo = project.GetProjectInfo();

if (string.Compare(e.Document.ActiveFile.FileTypeId, FiletypeConstants.FileTypeDefinitionId,
StringComparison.OrdinalIgnoreCase) != 0)
var firstDocument = e.Document.Files?.FirstOrDefault();
if (firstDocument == null)
{
return;
}

var documentIsLoadedOnce = DocumentIsLoadedOnce(e.Document);
if (documentIsLoadedOnce)
if (string.Compare(firstDocument.FileTypeId, FiletypeConstants.FileTypeDefinitionId,
StringComparison.OrdinalIgnoreCase) != 0)
{
return;
}
Expand All @@ -60,14 +59,13 @@ private void EditorController_Opening(object sender, CancelDocumentEventArgs e)
var documentSettings = new DocumentInfoSettings();
documentSettings.PopulateFromSettingsBundle(settings, FiletypeConstants.FileTypeDefinitionId);

var firstDocumenet = e.Document.Files.FirstOrDefault();
if (firstDocumenet != null && documentSettings.DocumentInfo.Any())
if (documentSettings.DocumentInfo.Any())
{
var documentInfo =
documentSettings.DocumentInfo.FirstOrDefault(a =>
string.Compare(a.Id, firstDocumenet.Id.ToString(), StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(a.Language, firstDocumenet.Language.CultureInfo.Name, StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(a.Name, firstDocumenet.Name, StringComparison.InvariantCultureIgnoreCase) == 0);
string.Compare(a.Id, firstDocument.Id.ToString(), StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(a.Language, firstDocument.Language.CultureInfo.Name, StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(a.Name, firstDocument.Name, StringComparison.InvariantCultureIgnoreCase) == 0);

if (documentInfo != null && documentInfo.IsLoadedOnce)
{
Expand All @@ -76,10 +74,10 @@ private void EditorController_Opening(object sender, CancelDocumentEventArgs e)

documentSettings.DocumentInfo.Add(new DocumentInfo
{
Id = firstDocumenet.Id.ToString(),
Id = firstDocument.Id.ToString(),
IsLoadedOnce = true,
Name = firstDocumenet.Name,
Language = firstDocumenet.Language.CultureInfo.Name
Name = firstDocument.Name,
Language = firstDocument.Language.CultureInfo.Name
});

documentSettings.SaveToSettingsBundle(settings, FiletypeConstants.FileTypeDefinitionId);
Expand Down Expand Up @@ -144,12 +142,5 @@ private void EditorController_Opening(object sender, CancelDocumentEventArgs e)
project.Save();
}
}

private static bool DocumentIsLoadedOnce(IStudioDocument document)
{
return document?.ActiveFileProperties?.Comments?.Comments != null &&
document.ActiveFileProperties.Comments.Comments.Any(
commentProperty => commentProperty.Author == "[MultilingualExcelLoaded]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.4.3")]
[assembly: AssemblyFileVersion("1.0.4.4")]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PluginPackage xmlns="http://www.sdl.com/Plugins/PluginPackage/1.0">
<PlugInName>Multilingual Excel FileType</PlugInName>
<Version>1.0.4.3</Version>
<Version>1.0.4.4</Version>
<Description>Multilingual Excel File Type</Description>
<Author>Trados AppStore Team</Author>
<RequiredProduct name="TradosStudio" minversion="17.0.1" maxversion="17.9"/>
Expand Down

0 comments on commit 0f2f8b4

Please sign in to comment.