Browse Source

Completion engine now works on freezed compilation units.

newNRvisualizers
mike 14 years ago
parent
commit
837d48d50a
  1. 2484
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs
  2. 35
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs
  3. 18
      ICSharpCode.NRefactory.CSharp/Completion/CSharpParameterCompletionEngine.cs
  4. 80
      ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs
  5. 1
      ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs
  6. 1
      ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ParameterCompletionTests.cs

2484
ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs

File diff suppressed because it is too large Load Diff

35
ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs

@ -453,10 +453,12 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -453,10 +453,12 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
using (var stream = new System.IO.StringReader (wrapper.ToString ())) {
try {
var parser = new CSharpParser ();
return parser.Parse(stream, "stub.cs", memberLocation.Line - 1 - generatedLines);
var result = parser.Parse(stream, "stub.cs", memberLocation.Line - 1 - generatedLines);
return result;
} catch (Exception) {
Console.WriteLine ("------");
Console.WriteLine (wrapper);
Console.WriteLine("------");
Console.WriteLine(wrapper);
throw;
}
}
@ -469,22 +471,24 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -469,22 +471,24 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
cachedText = null;
}
protected Tuple<string, TextLocation> GetMemberTextToCaret ()
protected Tuple<string, TextLocation> GetMemberTextToCaret()
{
int startOffset;
if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum) {
startOffset = document.GetOffset (currentMember.Region.Begin);
startOffset = document.GetOffset(currentMember.Region.Begin);
} else if (currentType != null) {
startOffset = document.GetOffset (currentType.Region.Begin);
startOffset = document.GetOffset(currentType.Region.Begin);
} else {
startOffset = 0;
}
while (startOffset > 0) {
char ch = document.GetCharAt (startOffset - 1);
if (ch != ' ' && ch != '\t')
char ch = document.GetCharAt(startOffset - 1);
if (ch != ' ' && ch != '\t') {
break;
}
--startOffset;
}
startOffset = 0;
if (cachedText == null)
cachedText = document.GetText (startOffset, offset - startOffset);
@ -498,13 +502,8 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -498,13 +502,8 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
baseUnit = ParseStub ("", false);
var section = baseUnit.GetNodeAt<AttributeSection> (location.Line, location.Column - 2);
var attr = section != null ? section.Attributes.LastOrDefault () : null;
if (attr != null) {
// insert target type into compilation unit, to respect the
attr.Remove ();
var node = Unit.GetNodeAt (location) ?? Unit;
node.AddChild (attr, Roles.Attribute);
return new ExpressionResult ((AstNode)attr, Unit);
}
if (attr != null)
return new ExpressionResult ((AstNode)attr, baseUnit);
}
if (currentMember == null && currentType == null) {
return null;
@ -529,11 +528,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -529,11 +528,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (expr == null)
return null;
}
var member = Unit.GetNodeAt<EntityDeclaration> (memberLocation);
var member2 = baseUnit.GetNodeAt<EntityDeclaration> (memberLocation);
member2.Remove ();
member.ReplaceWith (member2);
return new ExpressionResult ((AstNode)expr, Unit);
return new ExpressionResult ((AstNode)expr, baseUnit);
}
public class ExpressionResult

18
ICSharpCode.NRefactory.CSharp/Completion/CSharpParameterCompletionEngine.cs

@ -67,13 +67,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -67,13 +67,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return null;
}
var member = Unit.GetNodeAt<EntityDeclaration> (memberLocation);
var member2 = baseUnit.GetNodeAt<EntityDeclaration> (memberLocation);
if (member == null || member2 == null)
return null;
member2.Remove ();
member.ReplaceWith (member2);
return new ExpressionResult ((AstNode)expr, Unit);
return new ExpressionResult ((AstNode)expr, baseUnit);
}
public ExpressionResult GetConstructorInitializerBeforeCursor ()
@ -88,7 +82,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -88,7 +82,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
var expr = baseUnit.GetNodeAt <ConstructorInitializer> (location);
if (expr == null)
return null;
return new ExpressionResult ((AstNode)expr, Unit);
return new ExpressionResult ((AstNode)expr, baseUnit);
}
public ExpressionResult GetTypeBeforeCursor ()
@ -102,13 +96,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -102,13 +96,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
var memberLocation = currentMember != null ? currentMember.Region.Begin : currentType.Region.Begin;
var expr = baseUnit.GetNodeAt<AstType> (location.Line, location.Column + 1); // '>' position
var member = Unit.GetNodeAt<EntityDeclaration> (memberLocation);
var member2 = baseUnit.GetNodeAt<EntityDeclaration> (memberLocation);
if (member == null || member2 == null)
return null;
member2.Remove ();
member.ReplaceWith (member2);
return new ExpressionResult ((AstNode)expr, Unit);
return new ExpressionResult ((AstNode)expr, baseUnit);
}
IEnumerable<IMethod> CollectMethods (AstNode resolvedNode, MethodGroupResolveResult resolveResult)

80
ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

@ -66,46 +66,48 @@ namespace ICSharpCode.NRefactory.CSharp @@ -66,46 +66,48 @@ namespace ICSharpCode.NRefactory.CSharp
return new TextLocation (loc.Row, loc.Column);
}
public override void Visit (ModuleContainer mc)
public override void Visit(ModuleContainer mc)
{
bool first = true;
foreach (var container in mc.Containers) {
var nspace = container as NamespaceContainer;
if (nspace == null) {
container.Accept (this);
container.Accept(this);
continue;
}
NamespaceDeclaration nDecl = null;
var loc = LocationsBag.GetLocations (nspace);
var loc = LocationsBag.GetLocations(nspace);
if (nspace.NS != null && !string.IsNullOrEmpty (nspace.NS.Name)) {
if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
nDecl = new NamespaceDeclaration ();
if (loc != null)
nDecl.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.NamespaceKeyword);
ConvertNamespaceName (nspace.RealMemberName, nDecl);
if (loc != null && loc.Count > 1)
nDecl.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.LBrace);
AddToNamespace (nDecl);
namespaceStack.Push (nDecl);
if (loc != null) {
nDecl.AddChild(new CSharpTokenNode (Convert(loc [0])), Roles.NamespaceKeyword);
}
ConvertNamespaceName(nspace.RealMemberName, nDecl);
if (loc != null && loc.Count > 1) {
nDecl.AddChild(new CSharpTokenNode (Convert(loc [1])), Roles.LBrace);
}
AddToNamespace(nDecl);
namespaceStack.Push(nDecl);
}
if (nspace.Usings != null) {
foreach (var us in nspace.Usings) {
us.Accept (this);
us.Accept(this);
}
}
if (first) {
first = false;
AddAttributeSection (Unit, mc);
AddAttributeSection(Unit, mc);
}
if (nspace.Containers != null) {
foreach (var subContainer in nspace.Containers) {
subContainer.Accept (this);
subContainer.Accept(this);
}
}
Console.WriteLine("attr:" + mc.UnattachedAttributes);
if (nDecl != null) {
AddAttributeSection (nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
if (loc != null && loc.Count > 2)
@ -344,37 +346,38 @@ namespace ICSharpCode.NRefactory.CSharp @@ -344,37 +346,38 @@ namespace ICSharpCode.NRefactory.CSharp
return result;
}
public override void Visit (NamespaceContainer nspace)
public override void Visit(NamespaceContainer nspace)
{
NamespaceDeclaration nDecl = null;
var loc = LocationsBag.GetLocations (nspace);
var loc = LocationsBag.GetLocations(nspace);
if (nspace.NS != null && !string.IsNullOrEmpty (nspace.NS.Name)) {
if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
nDecl = new NamespaceDeclaration ();
if (loc != null)
nDecl.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.NamespaceKeyword);
ConvertNamespaceName (nspace.RealMemberName, nDecl);
if (loc != null && loc.Count > 1)
nDecl.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.LBrace);
AddToNamespace (nDecl);
namespaceStack.Push (nDecl);
if (loc != null) {
nDecl.AddChild(new CSharpTokenNode (Convert(loc [0])), Roles.NamespaceKeyword);
}
ConvertNamespaceName(nspace.RealMemberName, nDecl);
if (loc != null && loc.Count > 1) {
nDecl.AddChild(new CSharpTokenNode (Convert(loc [1])), Roles.LBrace);
}
AddToNamespace(nDecl);
namespaceStack.Push(nDecl);
}
if (nspace.Usings != null) {
foreach (var us in nspace.Usings) {
us.Accept (this);
us.Accept(this);
}
}
if (nspace.Containers != null) {
foreach (var container in nspace.Containers) {
container.Accept (this);
container.Accept(this);
}
}
if (nDecl != null) {
AddAttributeSection (nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
AddAttributeSection(nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
if (loc != null && loc.Count > 2)
nDecl.AddChild (new CSharpTokenNode (Convert (loc [2])), Roles.RBrace);
if (loc != null && loc.Count > 3)
@ -3606,17 +3609,20 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3606,17 +3609,20 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
public CompilationUnit Parse (CompilerCompilationUnit top, string fileName, int lineModifier = 0)
public CompilationUnit Parse(CompilerCompilationUnit top, string fileName, int lineModifier = 0)
{
if (top == null)
if (top == null) {
return null;
}
CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (GenerateTypeSystemMode, top.LocationsBag);
top.ModuleCompiled.Accept (conversionVisitor);
InsertComments (top, conversionVisitor);
if (CompilationUnitCallback != null)
CompilationUnitCallback (top);
if (top.LastYYValue is Mono.CSharp.Expression)
conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept (conversionVisitor) as AstNode;
top.ModuleCompiled.Accept(conversionVisitor);
InsertComments(top, conversionVisitor);
if (CompilationUnitCallback != null) {
CompilationUnitCallback(top);
}
if (top.LastYYValue is Mono.CSharp.Expression) {
conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept(conversionVisitor) as AstNode;
}
conversionVisitor.Unit.FileName = fileName;
return conversionVisitor.Unit;
}

1
ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs

@ -219,6 +219,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeCompletion @@ -219,6 +219,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeCompletion
pctx = pctx.AddAssemblyReferences (new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore });
var compilationUnit = new CSharpParser ().Parse (parsedText, "program.cs");
compilationUnit.Freeze ();
var parsedFile = compilationUnit.ToTypeSystem ();
pctx = pctx.UpdateProjectContent (null, parsedFile);

1
ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ParameterCompletionTests.cs

@ -271,6 +271,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeCompletion @@ -271,6 +271,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeCompletion
pctx = pctx.AddAssemblyReferences (new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore });
var compilationUnit = new CSharpParser ().Parse (parsedText, "program.cs");
compilationUnit.Freeze ();
var parsedFile = compilationUnit.ToTypeSystem ();
pctx = pctx.UpdateProjectContent (null, parsedFile);

Loading…
Cancel
Save