Browse Source

GetMemberTextToCaret now gives back the correct member start location.

newNRvisualizers
mike 14 years ago
parent
commit
40a00e7f86
  1. 54
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs

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

@ -415,49 +415,45 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
wrapper.Append (';'); wrapper.Append (';');
} }
protected CompilationUnit ParseStub (string continuation, bool appendSemicolon = true, string afterContinuation = null) protected CompilationUnit ParseStub(string continuation, bool appendSemicolon = true, string afterContinuation = null)
{ {
var mt = GetMemberTextToCaret (); var mt = GetMemberTextToCaret();
if (mt == null) if (mt == null) {
return null; return null;
}
string memberText = mt.Item1; string memberText = mt.Item1;
bool wrapInClass = mt.Item2; var memberLocation = mt.Item2;
int closingBrackets = 0;
int generatedLines = 0;
var wrapper = new StringBuilder (); var wrapper = new StringBuilder ();
bool wrapInClass = memberLocation != new TextLocation (1, 1);
if (wrapInClass) { if (wrapInClass) {
/* foreach (var child in Unit.Children) { /* foreach (var child in Unit.Children) {
if (child is UsingDeclaration) { if (child is UsingDeclaration) {
var offset = document.GetOffset (child.StartLocation); var offset = document.GetOffset (child.StartLocation);
wrapper.Append (document.GetText (offset, document.GetOffset (child.EndLocation) - offset)); wrapper.Append (document.GetText (offset, document.GetOffset (child.EndLocation) - offset));
} }
}*/ }*/
wrapper.Append ("class Stub {"); wrapper.Append("class Stub {");
wrapper.AppendLine (); wrapper.AppendLine();
closingBrackets++;
generatedLines = 1;
} }
wrapper.Append (memberText); wrapper.Append(memberText);
wrapper.Append (continuation); wrapper.Append(continuation);
AppendMissingClosingBrackets (wrapper, memberText, appendSemicolon); AppendMissingClosingBrackets(wrapper, memberText, appendSemicolon);
wrapper.Append (afterContinuation); wrapper.Append(afterContinuation);
if (wrapInClass) if (closingBrackets > 0) {
wrapper.Append ('}'); wrapper.Append(new string ('}', closingBrackets));
TextLocation memberLocation;
if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum) {
memberLocation = currentMember.Region.Begin;
} else if (currentType != null) {
memberLocation = currentType.Region.Begin;
} else {
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", memberLocation.Line - 1 - generatedLines);
} catch (Exception) { } catch (Exception) {
Console.WriteLine ("------"); Console.WriteLine ("------");
Console.WriteLine (wrapper); Console.WriteLine (wrapper);
@ -473,13 +469,13 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
cachedText = null; cachedText = null;
} }
protected Tuple<string, bool> GetMemberTextToCaret () protected Tuple<string, TextLocation> GetMemberTextToCaret ()
{ {
int startOffset; int startOffset;
if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum) { if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum) {
startOffset = document.GetOffset (currentMember.Region.BeginLine, currentMember.Region.BeginColumn); startOffset = document.GetOffset (currentMember.Region.Begin);
} else if (currentType != null) { } else if (currentType != null) {
startOffset = document.GetOffset (currentType.Region.BeginLine, currentType.Region.BeginColumn); startOffset = document.GetOffset (currentType.Region.Begin);
} else { } else {
startOffset = 0; startOffset = 0;
} }
@ -492,7 +488,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (cachedText == null) if (cachedText == null)
cachedText = document.GetText (startOffset, offset - startOffset); cachedText = document.GetText (startOffset, offset - startOffset);
return Tuple.Create (cachedText, startOffset != 0); return Tuple.Create (cachedText, document.GetLocation (startOffset));
} }
protected ExpressionResult GetInvocationBeforeCursor (bool afterBracket) protected ExpressionResult GetInvocationBeforeCursor (bool afterBracket)

Loading…
Cancel
Save