Browse Source

Fixed "type too fast" issue in code completion.

newNRvisualizers
Mike Krüger 15 years ago
parent
commit
d78d5f1727
  1. 36
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs
  2. 29
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs

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

@ -23,7 +23,6 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -416,6 +415,10 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
var contextList = new CompletionDataWrapper (this); var contextList = new CompletionDataWrapper (this);
var identifierStart = GetExpressionAtCursor (); var identifierStart = GetExpressionAtCursor ();
if (identifierStart != null && identifierStart.Item2 is TypeParameterDeclaration)
return null;
if (identifierStart != null && identifierStart.Item2 is VariableInitializer && location <= ((VariableInitializer)identifierStart.Item2).NameToken.EndLocation) { if (identifierStart != null && identifierStart.Item2 is VariableInitializer && location <= ((VariableInitializer)identifierStart.Item2).NameToken.EndLocation) {
return controlSpace ? HandleAccessorContext () ?? DefaultControlSpaceItems () : null; return controlSpace ? HandleAccessorContext () ?? DefaultControlSpaceItems () : null;
} }
@ -440,6 +443,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return HandleKeywordCompletion (tokenIndex, token); return HandleKeywordCompletion (tokenIndex, token);
} }
} }
if (identifierStart == null) if (identifierStart == null)
return HandleAccessorContext () ?? DefaultControlSpaceItems (); return HandleAccessorContext () ?? DefaultControlSpaceItems ();
@ -462,14 +466,14 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return DefaultControlSpaceItems (); return DefaultControlSpaceItems ();
return null; return null;
} }
if (n is ArrayInitializerExpression) { if (n is ArrayInitializerExpression) {
var initalizerResult = ResolveExpression (identifierStart.Item1, n.Parent, identifierStart.Item3); var initalizerResult = ResolveExpression (identifierStart.Item1, n.Parent, identifierStart.Item3);
var concreteNode = identifierStart.Item3.GetNodeAt<IdentifierExpression> (location); var concreteNode = identifierStart.Item3.GetNodeAt<IdentifierExpression> (location);
// check if we're on the right side of an initializer expression // check if we're on the right side of an initializer expression
if (concreteNode != null && concreteNode.Parent != null && concreteNode.Parent.Parent != null && concreteNode.Identifier != "a" && concreteNode.Parent.Parent is NamedExpression) if (concreteNode != null && concreteNode.Parent != null && concreteNode.Parent.Parent != null && concreteNode.Identifier != "a" && concreteNode.Parent.Parent is NamedExpression) {
return DefaultControlSpaceItems (); return DefaultControlSpaceItems ();
}
if (initalizerResult != null) { if (initalizerResult != null) {
@ -603,7 +607,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return null; return null;
} }
bool IsInLinqContext (int offset) bool IsInLinqContext (int offset)
{ {
string token; string token;
@ -698,7 +701,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
Predicate<IType> typePred = null; Predicate<IType> typePred = null;
if (node is Attribute) { if (node is Attribute) {
var attribute = Compilation.FindType (typeof (System.Attribute)); var attribute = Compilation.FindType (typeof(System.Attribute));
typePred = t => { typePred = t => {
return t.GetAllBaseTypeDefinitions ().Any (bt => bt.Equals (attribute)); return t.GetAllBaseTypeDefinitions ().Any (bt => bt.Equals (attribute));
}; };
@ -756,8 +759,9 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} }
} }
} }
if (currentMember != null) { if (this.currentMember != null) {
foreach (var member in ctx.CurrentTypeDefinition.GetMembers ()) { var def = ctx.CurrentTypeDefinition ?? Compilation.MainAssembly.GetTypeDefinition (currentType);
foreach (var member in def.GetMembers ()) {
if (memberPred == null || memberPred (member)) if (memberPred == null || memberPred (member))
wrapper.AddMember (member); wrapper.AddMember (member);
} }
@ -1019,7 +1023,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
foreach (var a in invoke.Arguments) { foreach (var a in invoke.Arguments) {
if (a == expressionOrVariableDeclaration.Item2) { if (a == expressionOrVariableDeclaration.Item2) {
if (mgr.Member.Parameters.Count > i1) if (mgr.Member.Parameters.Count > i1)
hintType = mgr.Member.Parameters[i1].Type; hintType = mgr.Member.Parameters [i1].Type;
break; break;
} }
i1++; i1++;
@ -1038,7 +1042,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
foreach (var a in invoke.Arguments) { foreach (var a in invoke.Arguments) {
if (a == expressionOrVariableDeclaration.Item2) { if (a == expressionOrVariableDeclaration.Item2) {
if (mgr.Member.Parameters.Count > i1) if (mgr.Member.Parameters.Count > i1)
hintType = mgr.Member.Parameters[i1].Type; hintType = mgr.Member.Parameters [i1].Type;
break; break;
} }
i1++; i1++;
@ -1225,7 +1229,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
addedVirtuals = true; addedVirtuals = true;
} }
if (!addedVirtuals) if (!addedVirtuals)
AddVirtuals (alreadyInserted, wrapper, type.Resolve (ctx).GetDefinition (), modifiers, Compilation.FindType(typeof(object)).GetDefinition (), declarationBegin); AddVirtuals (alreadyInserted, wrapper, type.Resolve (ctx).GetDefinition (), modifiers, Compilation.FindType (typeof(object)).GetDefinition (), declarationBegin);
return wrapper.Result; return wrapper.Result;
} }
@ -1875,6 +1879,18 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
baseUnit = tmpUnit; baseUnit = tmpUnit;
} }
// try parameter declaration type
if (expr == null) {
baseUnit = ParseStub (">", false, "{}");
expr = baseUnit.GetNodeAt<TypeParameterDeclaration> (location.Line, location.Column - 1);
}
// try parameter declaration method
if (expr == null) {
baseUnit = ParseStub ("> ()", false, "{}");
expr = baseUnit.GetNodeAt<TypeParameterDeclaration> (location.Line, location.Column - 1);
}
if (expr == null) if (expr == null)
return null; return null;
var member = Unit.GetNodeAt<AttributedNode> (memberLocation); var member = Unit.GetNodeAt<AttributedNode> (memberLocation);

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

@ -23,7 +23,6 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -59,6 +58,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
public IProjectContent ProjectContent { get; set; } public IProjectContent ProjectContent { get; set; }
ICompilation compilation; ICompilation compilation;
protected ICompilation Compilation { protected ICompilation Compilation {
get { get {
if (compilation == null) if (compilation == null)
@ -69,6 +69,16 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
#endregion #endregion
IUnresolvedTypeDefinition FindInnerType (IUnresolvedTypeDefinition parent, TextLocation location)
{
var currentType = parent;
foreach (var type in parent.NestedTypes) {
if (type.Region.Begin < location && location < type.Region.End)
currentType = FindInnerType (type, location);
}
return currentType;
}
protected void SetOffset (int offset) protected void SetOffset (int offset)
{ {
Reset (); Reset ();
@ -76,7 +86,14 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
this.offset = offset; this.offset = offset;
this.location = document.GetLocation (offset); this.location = document.GetLocation (offset);
this.currentType = CSharpParsedFile.GetInnermostTypeDefinition (location); this.currentType = null;
foreach (var type in CSharpParsedFile.TopLevelTypeDefinitions) {
if (type.Region.Begin < location)
currentType = type;
}
currentType = FindInnerType (currentType, location);
this.currentMember = null; this.currentMember = null;
if (this.currentType != null) { if (this.currentType != null) {
foreach (var member in currentType.Members) { foreach (var member in currentType.Members) {
@ -293,7 +310,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} }
} }
protected CompilationUnit ParseStub (string continuation, bool appendSemicolon = true) protected CompilationUnit ParseStub (string continuation, bool appendSemicolon = true, string afterContinuation = null)
{ {
var mt = GetMemberTextToCaret (); var mt = GetMemberTextToCaret ();
if (mt == null) if (mt == null)
@ -318,6 +335,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
wrapper.Append (memberText); wrapper.Append (memberText);
wrapper.Append (continuation); wrapper.Append (continuation);
AppendMissingClosingBrackets (wrapper, memberText, appendSemicolon); AppendMissingClosingBrackets (wrapper, memberText, appendSemicolon);
wrapper.Append (afterContinuation);
if (wrapInClass) if (wrapInClass)
wrapper.Append ('}'); wrapper.Append ('}');
@ -330,11 +348,12 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} else { } else {
memberLocation = new TextLocation (1, 1); memberLocation = new TextLocation (1, 1);
} }
using (var stream = new System.IO.StringReader (wrapper.ToString ())) { using (var stream = new System.IO.StringReader (wrapper.ToString ())) {
try { try {
var parser = new CSharpParser (); var parser = new CSharpParser ();
return parser.Parse (stream, "stub.cs" , wrapInClass ? memberLocation.Line - 2 : 0); return parser.Parse (stream, "stub.cs", wrapInClass ? memberLocation.Line - 2 : 0);
} catch (Exception){ } catch (Exception) {
Console.WriteLine ("------"); Console.WriteLine ("------");
Console.WriteLine (wrapper); Console.WriteLine (wrapper);
throw; throw;

Loading…
Cancel
Save