diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs index 704865e096..c2ecc02e16 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs @@ -231,71 +231,63 @@ namespace CSharpBinding.FormattingStrategy return false; } - - - bool IsBeforeRegion(TextArea textArea, DomRegion region, int lineNr) - { - if (region.IsEmpty) { - return false; - } - return region.BeginLine - 2 <= lineNr && lineNr <= region.BeginLine; - } - - object GetClassMember(TextArea textArea, int lineNr, IClass c) - { - if (IsBeforeRegion(textArea, c.Region, lineNr)) { - return c; - } - - foreach (IClass inner in c.InnerClasses) { - object o = GetClassMember(textArea, lineNr, inner); - if (o != null) { - return o; - } - } - - foreach (IField f in c.Fields) { - if (IsBeforeRegion(textArea, f.Region, lineNr)) { - return f; - } - } - foreach (IProperty p in c.Properties) { - if (IsBeforeRegion(textArea, p.Region, lineNr)) { - return p; - } - } - foreach (IEvent e in c.Events) { - if (IsBeforeRegion(textArea, e.Region, lineNr)) { - return e; - } - } - foreach (IMethod m in c.Methods) { - if (IsBeforeRegion(textArea, m.Region, lineNr)) { - return m; - } - } - return null; - } - - object GetMember(TextArea textArea, int lineNr) + /// + /// Gets the next member after the specified caret position. + /// + object GetMemberAfter(TextArea textArea, int caretLine) { string fileName = textArea.MotherTextEditorControl.FileName; + object nextElement = null; if (fileName != null && fileName.Length > 0 ) { - string fullPath = Path.GetFullPath(fileName); - ParseInformation parseInfo = ParserService.GetParseInformation(fullPath); + ParseInformation parseInfo = ParserService.ParseFile(fileName, textArea.Document.TextContent); if (parseInfo != null) { - ICompilationUnit currentCompilationUnit = (ICompilationUnit)parseInfo.BestCompilationUnit; + ICompilationUnit currentCompilationUnit = parseInfo.BestCompilationUnit; if (currentCompilationUnit != null) { - foreach (IClass c in currentCompilationUnit.Classes) { - object o = GetClassMember(textArea, lineNr, c); - if (o != null) { - return o; + IClass currentClass = currentCompilationUnit.GetInnermostClass(caretLine, 0); + int nextElementLine = int.MaxValue; + if (currentClass == null) { + foreach (IClass c in currentCompilationUnit.Classes) { + if (c.Region.BeginLine < nextElementLine && c.Region.BeginLine > caretLine) { + nextElementLine = c.Region.BeginLine; + nextElement = c; + } + } + } else { + foreach (IClass c in currentClass.InnerClasses) { + if (c.Region.BeginLine < nextElementLine && c.Region.BeginLine > caretLine) { + nextElementLine = c.Region.BeginLine; + nextElement = c; + } + } + foreach (IMember m in currentClass.Methods) { + if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) { + nextElementLine = m.Region.BeginLine; + nextElement = m; + } + } + foreach (IMember m in currentClass.Properties) { + if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) { + nextElementLine = m.Region.BeginLine; + nextElement = m; + } + } + foreach (IMember m in currentClass.Fields) { + if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) { + nextElementLine = m.Region.BeginLine; + nextElement = m; + } + } + foreach (IMember m in currentClass.Events) { + if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) { + nextElementLine = m.Region.BeginLine; + nextElement = m; + } } } } } } - return null; + return nextElement; } #endregion @@ -327,7 +319,7 @@ namespace CSharpBinding.FormattingStrategy string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); if (curLineText != null && curLineText.EndsWith("///") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("///"))) { string indentation = base.GetIndentation(textArea, lineNr); - object member = GetMember(textArea, lineNr); + object member = GetMemberAfter(textArea, lineNr); if (member != null) { StringBuilder sb = new StringBuilder(); sb.Append(" \n"); diff --git a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs index 0b62113a8d..7e91d273ca 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs +++ b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs @@ -269,7 +269,7 @@ namespace VBNetBinding.FormattingStrategy if (curLineText != null && curLineText.EndsWith("'''") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("'''"))) { string indentation = base.GetIndentation(textArea, lineNr); - object member = GetMember(textArea, lineNr); + object member = GetMemberAfter(textArea, lineNr); if (member != null) { StringBuilder sb = new StringBuilder(); sb.Append(" \n"); @@ -446,69 +446,63 @@ namespace VBNetBinding.FormattingStrategy return false; } - object GetMember(TextArea textArea, int lineNr) + /// + /// Gets the next member after the specified caret position. + /// + object GetMemberAfter(TextArea textArea, int caretLine) { string fileName = textArea.MotherTextEditorControl.FileName; + object nextElement = null; if (fileName != null && fileName.Length > 0 ) { - string fullPath = Path.GetFullPath(fileName); - ParseInformation parseInfo = ParserService.GetParseInformation(fullPath); + ParseInformation parseInfo = ParserService.ParseFile(fileName, textArea.Document.TextContent); if (parseInfo != null) { - ICompilationUnit currentCompilationUnit = (ICompilationUnit)parseInfo.BestCompilationUnit; + ICompilationUnit currentCompilationUnit = parseInfo.BestCompilationUnit; if (currentCompilationUnit != null) { - foreach (IClass c in currentCompilationUnit.Classes) { - object o = GetClassMember(textArea, lineNr, c); - if (o != null) { - return o; + IClass currentClass = currentCompilationUnit.GetInnermostClass(caretLine, 0); + int nextElementLine = int.MaxValue; + if (currentClass == null) { + foreach (IClass c in currentCompilationUnit.Classes) { + if (c.Region.BeginLine < nextElementLine && c.Region.BeginLine > caretLine) { + nextElementLine = c.Region.BeginLine; + nextElement = c; + } + } + } else { + foreach (IClass c in currentClass.InnerClasses) { + if (c.Region.BeginLine < nextElementLine && c.Region.BeginLine > caretLine) { + nextElementLine = c.Region.BeginLine; + nextElement = c; + } + } + foreach (IMember m in currentClass.Methods) { + if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) { + nextElementLine = m.Region.BeginLine; + nextElement = m; + } + } + foreach (IMember m in currentClass.Properties) { + if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) { + nextElementLine = m.Region.BeginLine; + nextElement = m; + } + } + foreach (IMember m in currentClass.Fields) { + if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) { + nextElementLine = m.Region.BeginLine; + nextElement = m; + } + } + foreach (IMember m in currentClass.Events) { + if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) { + nextElementLine = m.Region.BeginLine; + nextElement = m; + } } } } } } - return null; - } - - object GetClassMember(TextArea textArea, int lineNr, IClass c) - { - if (IsBeforeRegion(textArea, c.Region, lineNr)) { - return c; - } - - foreach (IClass inner in c.InnerClasses) { - object o = GetClassMember(textArea, lineNr, inner); - if (o != null) { - return o; - } - } - - foreach (IField f in c.Fields) { - if (IsBeforeRegion(textArea, f.Region, lineNr)) { - return f; - } - } - foreach (IProperty p in c.Properties) { - if (IsBeforeRegion(textArea, p.Region, lineNr)) { - return p; - } - } - foreach (IEvent e in c.Events) { - if (IsBeforeRegion(textArea, e.Region, lineNr)) { - return e; - } - } - foreach (IMethod m in c.Methods) { - if (IsBeforeRegion(textArea, m.Region, lineNr)) { - return m; - } - } - return null; - } - - bool IsBeforeRegion(TextArea textArea, DomRegion region, int lineNr) - { - if (region.IsEmpty) { - return false; - } - return region.BeginLine - 2 <= lineNr && lineNr <= region.BeginLine; + return nextElement; } bool IsInString(string start)