Skip to content

Commit b0404ca

Browse files
Guard against null root - fixes #457 (followup)
1 parent 800068b commit b0404ca

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
88

99
### Vsix
1010

11+
* Fixed error caused when converting with "Copy to clipboard" option enabled
12+
1113
### VB -> C#
1214
* Add modifier for nested types
1315
* Remove body for converted extern methods

ICSharpCode.CodeConverter/CSharp/VisualBasicConverter.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using Microsoft.CodeAnalysis;
34
using Microsoft.CodeAnalysis.CSharp;
45
using Microsoft.CodeAnalysis.Editing;
@@ -15,11 +16,21 @@ public static async Task<SyntaxNode> ConvertCompilationTree(Document document,
1516
var compilation = await document.Project.GetCompilationAsync();
1617
var tree = await document.GetSyntaxTreeAsync();
1718
var semanticModel = compilation.GetSemanticModel(tree, true);
18-
var root = (VBasic.VisualBasicSyntaxNode)await document.GetSyntaxRootAsync();
19+
var root = await document.GetSyntaxRootAsync() as VBasic.VisualBasicSyntaxNode ??
20+
throw new InvalidOperationException(NullRootError(document));
21+
1922
var csSyntaxGenerator = SyntaxGenerator.GetGenerator(csharpReferenceProject);
2023
var visualBasicSyntaxVisitor = new
2124
DeclarationNodeVisitor(document, compilation, semanticModel, csharpViewOfVbSymbols, csSyntaxGenerator);
2225
return await root.Accept(visualBasicSyntaxVisitor.TriviaConvertingVisitor);
2326
}
27+
28+
private static string NullRootError(Document document)
29+
{
30+
string initial = document.Project.Language != LanguageNames.VisualBasic
31+
? "Document cannot be converted because it's not within a VB project."
32+
: "Could not find valid VB within document.";
33+
return initial + " For best results, convert a VB document from within a VB project which compiles successfully.";
34+
}
2435
}
2536
}

ICSharpCode.CodeConverter/VB/CSharpConverter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using Microsoft.CodeAnalysis;
34
using Microsoft.CodeAnalysis.Editing;
45
using Microsoft.CodeAnalysis.VisualBasic;
@@ -15,11 +16,20 @@ public static async Task<SyntaxNode> ConvertCompilationTree(Document document,
1516
var compilation = await document.Project.GetCompilationAsync();
1617
var tree = await document.GetSyntaxTreeAsync();
1718
var semanticModel = compilation.GetSemanticModel(tree, true);
18-
var root = (CS.CSharpSyntaxNode) await document.GetSyntaxRootAsync();
19+
var root = await document.GetSyntaxRootAsync() as CS.CSharpSyntaxNode ??
20+
throw new InvalidOperationException(NullRootError(document));
1921

2022
var vbSyntaxGenerator = SyntaxGenerator.GetGenerator(vbReferenceProject);
2123
var visualBasicSyntaxVisitor = new NodesVisitor(document, (CS.CSharpCompilation) compilation, semanticModel, vbViewOfCsSymbols, vbSyntaxGenerator);
2224
return root.Accept(visualBasicSyntaxVisitor.TriviaConvertingVisitor);
2325
}
26+
27+
private static string NullRootError(Document document)
28+
{
29+
var initial = document.Project.Language != LanguageNames.CSharp
30+
? "Document cannot be converted because it's not within a C# project."
31+
: "Could not find valid C# within document.";
32+
return initial + " For best results, convert a c# document from within a C# project which compiles successfully.";
33+
}
2434
}
2535
}

0 commit comments

Comments
 (0)