Browse Source

ported VB.NET formatting strategy to ITextEditor

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4794 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 17 years ago
parent
commit
beb31a729a
  1. 484
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs
  2. 2
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBStatement.cs
  3. 23
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/VBNetLanguageBinding.cs
  4. 6
      src/AddIns/BackendBindings/VBNetBinding/Project/VBNetBinding.addin
  5. 1
      src/AddIns/BackendBindings/VBNetBinding/Project/VBNetBinding.csproj
  6. 424
      src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndOperatorTests.cs
  7. 73
      src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndSubTests.cs
  8. 40
      src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs
  9. 34
      src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/InterfaceTests.cs
  10. 19
      src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj

484
src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs

@ -1,15 +1,13 @@ @@ -1,15 +1,13 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Markus Palme" email="MarkusPalme@gmx.de"/>
// <owner name="Siegfried Pammer" email="siegfriedpammer@gmail.com"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@ -19,15 +17,15 @@ using ICSharpCode.NRefactory.Parser; @@ -19,15 +17,15 @@ using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Parser.VB;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Editor;
namespace VBNetBinding.FormattingStrategy
namespace VBNetBinding
{
/*
/// <summary>
/// This class handles the auto and smart indenting in the textbuffer while
/// you type.
/// </summary>
public class VBFormattingStrategy : DefaultFormattingStrategy
public class VBNetFormattingStrategy : DefaultFormattingStrategy
{
List<VBStatement> statements;
IList<string> keywords;
@ -44,7 +42,7 @@ namespace VBNetBinding.FormattingStrategy @@ -44,7 +42,7 @@ namespace VBNetBinding.FormattingStrategy
bool doCasing;
bool doInsertion;
public VBFormattingStrategy()
public VBNetFormattingStrategy()
{
statements = new List<VBStatement>();
statements.Add(new VBStatement(@"^if.*?(then|\s+_)$", "^end ?if$", "End If", 1));
@ -116,219 +114,220 @@ namespace VBNetBinding.FormattingStrategy @@ -116,219 +114,220 @@ namespace VBNetBinding.FormattingStrategy
};
}
public override void FormatLine(TextArea textArea, int lineNr, int cursorOffset, char ch) // used for comment tag formater/inserter
public override void FormatLine(ITextEditor editor, char charTyped)
{
textArea.Document.UndoStack.StartUndoGroup();
FormatLineInternal(textArea, lineNr, cursorOffset, ch);
textArea.Document.UndoStack.EndUndoGroup();
using (editor.Document.OpenUndoGroup()) {
FormatLineInternal(editor, editor.Caret.Line, editor.Caret.Offset, charTyped);
}
}
void FormatLineInternal(TextArea textArea, int lineNr, int cursorOffset, char ch)
void FormatLineInternal(ITextEditor editor, int lineNr, int cursorOffset, char ch)
{
string terminator = textArea.TextEditorProperties.LineTerminator;
string terminator = DocumentUtilitites.GetLineTerminator(editor.Document, lineNr);
doCasing = PropertyService.Get("VBBinding.TextEditor.EnableCasing", true);
doInsertion = PropertyService.Get("VBBinding.TextEditor.EnableEndConstructs", true);
if (lineNr > 0)
IDocumentLine currentLine = editor.Document.GetLine(lineNr);
IDocumentLine lineAbove = lineNr > 1 ? editor.Document.GetLine(lineNr - 1) : null;
string curLineText = editor.Document.Text;
string lineAboveText = lineAbove == null ? "" : lineAbove.Text;
if (ch == '\'') {
InsertDocumentationComments(editor, lineNr, cursorOffset, ch);
}
if (ch == '\n' && lineAboveText != null)
{
LineSegment curLine = textArea.Document.GetLineSegment(lineNr);
LineSegment lineAbove = lineNr > 0 ? textArea.Document.GetLineSegment(lineNr - 1) : null;
string textToReplace = lineAboveText;
// remove string content
MatchCollection strmatches = Regex.Matches(textToReplace, "\"[^\"]*?\"", RegexOptions.Singleline);
foreach (Match match in strmatches)
{
textToReplace = textToReplace.Remove(match.Index, match.Length).Insert(match.Index, new String('-', match.Length));
}
// remove comments
textToReplace = Regex.Replace(textToReplace, "'.*$", "", RegexOptions.Singleline);
string curLineText = textArea.Document.GetText(curLine.Offset, curLine.Length);
string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove);
if (doCasing)
DoCasingOnLine(lineAbove, textToReplace, editor);
if (ch == '\'') {
curLineText = textArea.Document.GetText(curLine);
if (curLineText != null && curLineText.EndsWith("'''") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("'''"))) {
string indentation = base.GetIndentation(textArea, lineNr);
object member = GetMemberAfter(textArea, lineNr);
if (member != null) {
StringBuilder sb = new StringBuilder();
sb.Append(" <summary>");
sb.Append(terminator);
sb.Append(indentation);
sb.Append("''' ");
sb.Append(terminator);
sb.Append(indentation);
sb.Append("''' </summary>");
if (member is IMethod) {
IMethod method = (IMethod)member;
if (method.Parameters != null && method.Parameters.Count > 0) {
for (int i = 0; i < method.Parameters.Count; ++i) {
sb.Append(terminator);
sb.Append(indentation);
sb.Append("''' <param name=\"");
sb.Append(method.Parameters[i].Name);
sb.Append("\"></param>");
}
}
if (method.ReturnType != null && !method.IsConstructor && method.ReturnType.FullyQualifiedName != "System.Void") {
sb.Append(terminator);
sb.Append(indentation);
sb.Append("''' <returns></returns>");
}
}
textArea.Document.Insert(cursorOffset, sb.ToString());
textArea.Refresh();
textArea.Caret.Position = textArea.Document.OffsetToPosition(cursorOffset + indentation.Length + "/// ".Length + " <summary>".Length + terminator.Length);
}
if (doInsertion)
DoInsertionOnLine(terminator, currentLine, lineAbove, textToReplace, editor, lineNr);
if (IsInString(lineAboveText)) {
if (IsFinishedString(curLineText)) {
editor.Document.Insert(lineAbove.Offset + lineAbove.Length,
"\" & _");
editor.Document.Insert(currentLine.Offset, "\"");
} else {
editor.Document.Insert(lineAbove.Offset + lineAbove.Length,
"\"");
}
return;
} else {
string indent = DocumentUtilitites.GetIndentation(editor.Document, lineAbove.Offset);
if (indent.Length > 0) {
string newLineText = indent + currentLine.Text.Trim();
editor.Document.Replace(currentLine.Offset, currentLine.Length, newLineText);
}
editor.Caret.Column = indent.Length + 1;
}
if (ch == '\n' && lineAboveText != null)
IndentLines(editor, lineNr - 1, lineNr);
}
else if(ch == '>')
{
if (IsInsideDocumentationComment(editor, currentLine, cursorOffset))
{
string texttoreplace = lineAboveText;
// remove string content
MatchCollection strmatches = Regex.Matches(texttoreplace, "\"[^\"]*?\"", RegexOptions.Singleline);
foreach (Match match in strmatches)
{
texttoreplace = texttoreplace.Remove(match.Index, match.Length).Insert(match.Index, new String('-', match.Length));
}
// remove comments
texttoreplace = Regex.Replace(texttoreplace, "'.*$", "", RegexOptions.Singleline);
int column = editor.Caret.Offset - currentLine.Offset;
int index = Math.Min(column - 1, curLineText.Length - 1);
if (doCasing)
{
foreach (string keyword in keywords) {
string regex = "\\b" + keyword + "\\b"; // \b = word border
MatchCollection matches = Regex.Matches(texttoreplace, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (Match match in matches) {
if (keyword == "EndIf") // special case
textArea.Document.Replace(lineAbove.Offset + match.Index, match.Length, "End If");
else
textArea.Document.Replace(lineAbove.Offset + match.Index, match.Length, keyword);
}
}
while (index > 0 && curLineText[index] != '<') {
--index;
if(curLineText[index] == '/')
return; // the tag was an end tag or already
}
if (doInsertion)
{
if (Regex.IsMatch(texttoreplace.Trim(), @"^If .*[^_]$", RegexOptions.IgnoreCase)) {
if (!Regex.IsMatch(texttoreplace, @"\bthen\b", RegexOptions.IgnoreCase)) {
string specialThen = "Then"; // do special check in cases like If t = True' comment
if (textArea.Document.GetCharAt(lineAbove.Offset + texttoreplace.Length) == '\'')
specialThen += " ";
if (textArea.Document.GetCharAt(lineAbove.Offset + texttoreplace.Length - 1) != ' ')
specialThen = " " + specialThen;
textArea.Document.Insert(lineAbove.Offset + texttoreplace.Length, specialThen);
texttoreplace += specialThen;
}
if (index > 0) {
StringBuilder commentBuilder = new StringBuilder("");
for (int i = index; i < curLineText.Length && i < column && !Char.IsWhiteSpace(curLineText[i]); ++i) {
commentBuilder.Append(curLineText[ i]);
}
// check #Region statements
if (Regex.IsMatch(texttoreplace.Trim(), @"^#Region", RegexOptions.IgnoreCase) && LookForEndRegion(textArea)) {
string indentation = GetIndentation(textArea, lineNr - 1);
texttoreplace += indentation + "\r\n" + indentation + "#End Region";
textArea.Document.Replace(curLine.Offset, curLine.Length, texttoreplace);
string tag = commentBuilder.ToString().Trim();
if (!tag.EndsWith(">")) {
tag += ">";
}
foreach (VBStatement statement_ in statements) {
VBStatement statement = statement_; // allow passing statement byref
if (Regex.IsMatch(texttoreplace.Trim(), statement.StartRegex, RegexOptions.IgnoreCase)) {
string indentation = GetIndentation(textArea, lineNr - 1);
if (IsEndStatementNeeded(textArea, ref statement, lineNr))
textArea.Document.Replace(curLine.Offset, curLine.Length, terminator + indentation + statement.EndStatement);
if (!IsInsideInterface(textArea, lineNr) || statement == interfaceStatement) {
for (int i = 0; i < statement.IndentPlus; i++) {
indentation += Tab.GetIndentationString(textArea.Document);
}
}
textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + curLineText.Trim());
textArea.Caret.Column = indentation.Length;
return;
}
if (!tag.StartsWith("/")) {
editor.Document.Insert(editor.Caret.Offset, "</" + tag.Substring(1));
}
}
if (IsInString(lineAboveText))
{
if (IsFinishedString(curLineText)) {
textArea.Document.Insert(lineAbove.Offset + lineAbove.Length,
"\" & _");
curLine = textArea.Document.GetLineSegment(lineNr);
textArea.Document.Insert(curLine.Offset, "\"");
if (IsElseConstruct(lineAboveText))
SmartIndentLine(textArea, lineNr - 1);
textArea.Caret.Column = SmartIndentLine(textArea, lineNr) + 1;
} else {
textArea.Document.Insert(lineAbove.Offset + lineAbove.Length,
"\"");
if (IsElseConstruct(lineAboveText))
SmartIndentLine(textArea, lineNr - 1);
textArea.Caret.Column = SmartIndentLine(textArea, lineNr);
}
}
}
}
void DoInsertionOnLine(string terminator, IDocumentLine currentLine, IDocumentLine lineAbove, string textToReplace, ITextEditor editor, int lineNr)
{
string curLineText = currentLine.Text;
if (Regex.IsMatch(textToReplace.Trim(), "^If .*[^_]$", RegexOptions.IgnoreCase)) {
if (!Regex.IsMatch(textToReplace, "\\bthen\\b", RegexOptions.IgnoreCase)) {
string specialThen = "Then"; // do special check in cases like If t = True' comment
if (editor.Document.GetCharAt(lineAbove.Offset + textToReplace.Length) == '\'')
specialThen += " ";
if (editor.Document.GetCharAt(lineAbove.Offset + textToReplace.Length - 1) != ' ')
specialThen = " " + specialThen;
editor.Document.Insert(lineAbove.Offset + textToReplace.Length, specialThen);
textToReplace += specialThen;
}
}
// check #Region statements
if (Regex.IsMatch(textToReplace.Trim(), "^#Region", RegexOptions.IgnoreCase) && LookForEndRegion(editor)) {
string indentation = DocumentUtilitites.GetIndentation(editor.Document, lineAbove.Offset);
textToReplace += indentation + "\r\n" + indentation + "#End Region";
editor.Document.Replace(currentLine.Offset, currentLine.Length, textToReplace);
}
foreach (VBStatement statement_ in statements) {
VBStatement statement = statement_; // allow passing statement byref
if (Regex.IsMatch(textToReplace.Trim(), statement.StartRegex, RegexOptions.IgnoreCase)) {
string indentation = DocumentUtilitites.GetIndentation(editor.Document, lineAbove.Offset);
if (IsEndStatementNeeded(editor, ref statement, lineNr)) {
editor.Document.Replace(currentLine.Offset, currentLine.Length, terminator + indentation + statement.EndStatement);
}
else
{
string indent = GetIndentation(textArea, lineNr - 1);
if (indent.Length > 0) {
string newLineText = indent + TextUtilities.GetLineAsString(textArea.Document, lineNr).Trim();
curLine = textArea.Document.GetLineSegment(lineNr);
textArea.Document.Replace(curLine.Offset, curLine.Length, newLineText);
if (!IsInsideInterface(editor, lineNr) || statement == interfaceStatement) {
for (int i = 0; i < statement.IndentPlus; i++) {
indentation += editor.Options.IndentationString;
}
if (IsElseConstruct(lineAboveText))
SmartIndentInternal(textArea, lineNr - 1, lineNr);
textArea.Caret.Column = GetIndentation(textArea, lineNr).Length;
}
editor.Document.Replace(currentLine.Offset, currentLine.Length, indentation + curLineText.Trim());
editor.Caret.Line = currentLine.LineNumber;
editor.Caret.Column = indentation.Length;
return;
}
else if(ch == '>')
{
if (IsInsideDocumentationComment(textArea, curLine, cursorOffset))
{
curLineText = textArea.Document.GetText(curLine);
int column = textArea.Caret.Offset - curLine.Offset;
int index = Math.Min(column - 1, curLineText.Length - 1);
while (index > 0 && curLineText[index] != '<') {
--index;
if(curLineText[index] == '/')
return; // the tag was an end tag or already
}
if (index > 0) {
StringBuilder commentBuilder = new StringBuilder("");
for (int i = index; i < curLineText.Length && i < column && !Char.IsWhiteSpace(curLineText[i]); ++i) {
commentBuilder.Append(curLineText[ i]);
}
string tag = commentBuilder.ToString().Trim();
if (!tag.EndsWith(">")) {
tag += ">";
}
if (!tag.StartsWith("/")) {
textArea.Document.Insert(textArea.Caret.Offset, "</" + tag.Substring(1));
}
}
void DoCasingOnLine(IDocumentLine lineAbove, string textToReplace, ITextEditor editor)
{
foreach (string keyword in keywords) {
string regex = "\\b" + keyword + "\\b"; // \b = word border
MatchCollection matches = Regex.Matches(textToReplace, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (Match match in matches) {
if (keyword == "EndIf") // special case
editor.Document.Replace(lineAbove.Offset + match.Index, match.Length, "End If");
else
editor.Document.Replace(lineAbove.Offset + match.Index, match.Length, keyword);
}
}
}
void InsertDocumentationComments(ITextEditor editor, int lineNr, int cursorOffset, char ch)
{
string terminator = DocumentUtilitites.GetLineTerminator(editor.Document, lineNr);
IDocumentLine currentLine = editor.Document.GetLine(lineNr);
IDocumentLine previousLine = (lineNr > 1) ? editor.Document.GetLine(lineNr - 1) : null;
string curLineText = currentLine.Text;
string lineAboveText = previousLine == null ? null : previousLine.Text;
if (curLineText != null && curLineText.EndsWith("'''") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("'''"))) {
string indentation = DocumentUtilitites.GetIndentation(editor.Document, currentLine.Offset);
object member = GetMemberAfter(editor, lineNr);
if (member != null) {
StringBuilder sb = new StringBuilder();
sb.Append(" <summary>");
sb.Append(terminator);
sb.Append(indentation);
sb.Append("''' ");
sb.Append(terminator);
sb.Append(indentation);
sb.Append("''' </summary>");
if (member is IMethod) {
IMethod method = (IMethod)member;
if (method.Parameters != null && method.Parameters.Count > 0) {
for (int i = 0; i < method.Parameters.Count; ++i) {
sb.Append(terminator);
sb.Append(indentation);
sb.Append("''' <param name=\"");
sb.Append(method.Parameters[i].Name);
sb.Append("\"></param>");
}
}
if (method.ReturnType != null && !method.IsConstructor && method.ReturnType.FullyQualifiedName != "System.Void") {
sb.Append(terminator);
sb.Append(indentation);
sb.Append("''' <returns></returns>");
}
}
editor.Document.Insert(cursorOffset, sb.ToString());
editor.Caret.Position = editor.Document.OffsetToPosition(cursorOffset + indentation.Length + "/// ".Length + " <summary>".Length + terminator.Length);
}
}
}
bool LookForEndRegion(TextArea area)
bool LookForEndRegion(ITextEditor editor)
{
string lineText = area.Document.GetText(area.Document.GetLineSegment(0));
string lineText = editor.Document.GetLine(1).Text;
int count = 0;
int lineNr = 0;
while ((!Regex.IsMatch(lineText, @"^\s*#End\s+Region", RegexOptions.IgnoreCase) || count >= 0) && (lineNr < area.Document.TotalNumberOfLines)) {
while ((!Regex.IsMatch(lineText, @"^\s*#End\s+Region", RegexOptions.IgnoreCase) || count >= 0) && (lineNr < editor.Document.TotalNumberOfLines)) {
if (Regex.IsMatch(lineText, @"^\s*#Region", RegexOptions.IgnoreCase))
count++;
if (Regex.IsMatch(lineText, @"^\s*#End\s+Region", RegexOptions.IgnoreCase))
count--;
lineNr++;
if (lineNr < area.Document.TotalNumberOfLines)
lineText = area.Document.GetText(area.Document.GetLineSegment(lineNr));
if (lineNr < editor.Document.TotalNumberOfLines)
lineText = editor.Document.GetLine(1).Text;
}
return (count > 0);
}
bool IsInsideInterface(TextArea area, int lineNr)
bool IsInsideInterface(ITextEditor editor, int lineNr)
{
ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(area.Document.TextContent));
ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(editor.Document.Text));
Stack<Token> tokens = new Stack<Token>();
@ -409,12 +408,12 @@ namespace VBNetBinding.FormattingStrategy @@ -409,12 +408,12 @@ namespace VBNetBinding.FormattingStrategy
(type == Tokens.Interface);
}
bool IsEndStatementNeeded(TextArea textArea, ref VBStatement statement, int lineNr)
bool IsEndStatementNeeded(ITextEditor editor, ref VBStatement statement, int lineNr)
{
Stack<Token> tokens = new Stack<Token>();
List<Token> missingEnds = new List<Token>();
ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(textArea.Document.TextContent));
ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(editor.Document.Text));
Token currentToken = null;
Token prevToken = null;
@ -446,23 +445,23 @@ namespace VBNetBinding.FormattingStrategy @@ -446,23 +445,23 @@ namespace VBNetBinding.FormattingStrategy
missingEnds.Add(tokens.Pop());
if (missingEnds.Count > 0)
return GetClosestMissing(missingEnds, statement, textArea, lineNr) != null;
return GetClosestMissing(missingEnds, statement, editor, lineNr) != null;
else
return false;
}
Token GetClosestMissing(List<Token> missingEnds, VBStatement statement, TextArea textArea, int lineNr)
Token GetClosestMissing(List<Token> missingEnds, VBStatement statement, ITextEditor editor, int lineNr)
{
Token closest = null;
int diff = 0;
foreach (Token t in missingEnds) {
if (!IsSingleLine(t.Location.Line, textArea)) {
if (IsMatchingStatement(t, statement) && ((diff = lineNr - t.Location.Line + 1) > -1)) {
if (!IsSingleLine(t.Location.Line, editor)) {
if (IsMatchingStatement(t, statement) && ((diff = lineNr - t.Location.Line) > 0)) {
if (closest == null) {
closest = t;
} else {
if (diff < lineNr - closest.Location.Line + 1)
if (diff < lineNr - closest.Location.Line)
closest = t;
}
}
@ -472,17 +471,17 @@ namespace VBNetBinding.FormattingStrategy @@ -472,17 +471,17 @@ namespace VBNetBinding.FormattingStrategy
return closest;
}
bool IsSingleLine(int line, TextArea textArea)
bool IsSingleLine(int line, ITextEditor editor)
{
if (line < 1)
return false;
LineSegment lineSeg = textArea.Document.GetLineSegment(line - 1);
IDocumentLine lineSeg = editor.Document.GetLine(line);
if (lineSeg == null)
return false;
string text = textArea.Document.GetText(lineSeg);
string text = lineSeg.Text;
if (StripComment(text).Trim(' ', '\t', '\r', '\n').EndsWith("_"))
return true;
@ -523,14 +522,14 @@ namespace VBNetBinding.FormattingStrategy @@ -523,14 +522,14 @@ namespace VBNetBinding.FormattingStrategy
return Regex.Replace(text, "'.*$", "", RegexOptions.Singleline).Trim();
}
bool IsInsideDocumentationComment(TextArea textArea, LineSegment curLine, int cursorOffset)
bool IsInsideDocumentationComment(ITextEditor editor, IDocumentLine curLine, int cursorOffset)
{
for (int i = curLine.Offset; i < cursorOffset; ++i) {
char ch = textArea.Document.GetCharAt(i);
char ch = editor.Document.GetCharAt(i);
if (ch == '"') {
return false;
}
if (ch == '\'' && i + 2 < cursorOffset && textArea.Document.GetCharAt(i + 1) == '\'' && textArea.Document.GetCharAt(i + 2) == '\'')
if (ch == '\'' && i + 2 < cursorOffset && editor.Document.GetCharAt(i + 1) == '\'' && editor.Document.GetCharAt(i + 2) == '\'')
{
return true;
}
@ -538,25 +537,20 @@ namespace VBNetBinding.FormattingStrategy @@ -538,25 +537,20 @@ namespace VBNetBinding.FormattingStrategy
return false;
}
public override void IndentLines(TextArea textArea, int begin, int end)
public override void IndentLines(ITextEditor editor, int begin, int end)
{
if (textArea.Document.TextEditorProperties.IndentStyle != IndentStyle.Smart) {
base.IndentLines(textArea, begin, end);
return;
}
SmartIndentInternal(textArea, begin, end);
SmartIndentInternal(editor, begin, end);
}
int SmartIndentInternal(TextArea textArea, int begin, int end)
int SmartIndentInternal(ITextEditor editor, int begin, int end)
{
ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(textArea.Document.TextContent));
ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(editor.Document.Text));
Stack<string> indentation = new Stack<string>();
indentation.Push(string.Empty);
int oldLine = 0;
int oldLine = 1;
bool inInterface = false;
bool isMustOverride = false;
@ -583,16 +577,16 @@ namespace VBNetBinding.FormattingStrategy @@ -583,16 +577,16 @@ namespace VBNetBinding.FormattingStrategy
isDelegate = isDeclare = isMustOverride = false;
if (IsSpecialCase(currentToken, prevToken)) {
ApplyToRange(textArea, indentation, oldLine, currentToken.Location.Line, begin, end);
ApplyToRange(editor, indentation, oldLine, currentToken.Location.Line, begin, end);
Unindent(indentation);
ApplyToRange(textArea, indentation, currentToken.Location.Line - 1, currentToken.Location.Line, begin, end);
Indent(textArea, indentation);
ApplyToRange(editor, indentation, currentToken.Location.Line, currentToken.Location.Line, begin, end);
Indent(editor, indentation);
oldLine = currentToken.Location.Line;
oldLine = currentToken.Location.Line + 1;
}
if (IsBlockEnd(currentToken, prevToken)) {
ApplyToRange(textArea, indentation, oldLine, currentToken.Location.Line - 1, begin, end);
ApplyToRange(editor, indentation, oldLine, currentToken.Location.Line, begin, end);
if (currentToken.Kind == Tokens.Interface)
inInterface = false;
@ -604,24 +598,24 @@ namespace VBNetBinding.FormattingStrategy @@ -604,24 +598,24 @@ namespace VBNetBinding.FormattingStrategy
Unindent(indentation);
}
oldLine = currentToken.Location.Line - 1;
oldLine = currentToken.Location.Line;
}
if (IsBlockStart(lexer, currentToken, prevToken)) {
int line = GetLastVisualLine(currentToken.Location.Line, textArea);
ApplyToRange(textArea, indentation, oldLine, line, begin, end);
int line = GetLastVisualLine(currentToken.Location.Line, editor);
ApplyToRange(editor, indentation, oldLine, line, begin, end);
if (!inInterface && !isMustOverride && !isDeclare && !isDelegate) {
Indent(textArea, indentation);
Indent(editor, indentation);
if (currentToken.Kind == Tokens.Select)
Indent(textArea, indentation);
Indent(editor, indentation);
}
if (currentToken.Kind == Tokens.Interface)
inInterface = true;
oldLine = line;
oldLine = line + 1;
}
prevToken = currentToken;
@ -631,19 +625,19 @@ namespace VBNetBinding.FormattingStrategy @@ -631,19 +625,19 @@ namespace VBNetBinding.FormattingStrategy
int newLine = prevToken.Location.Line;
if (oldLine > newLine)
newLine = oldLine + 1;
newLine = oldLine;
ApplyToRange(textArea, indentation, oldLine, newLine, begin, end);
ApplyToRange(editor, indentation, oldLine, newLine, begin, end);
return (indentation.PeekOrDefault() ?? string.Empty).Length;
}
int GetLastVisualLine(int line, TextArea area)
int GetLastVisualLine(int line, ITextEditor area)
{
string text = StripComment(area.Document.GetText(area.Document.GetLineSegment(line - 1)));
string text = StripComment(area.Document.GetLine(line).Text);
while (text.EndsWith("_", StringComparison.Ordinal)) {
line++;
text = StripComment(area.Document.GetText(area.Document.GetLineSegment(line - 1)));
text = StripComment(area.Document.GetLine(line).Text);
}
return line;
}
@ -653,10 +647,10 @@ namespace VBNetBinding.FormattingStrategy @@ -653,10 +647,10 @@ namespace VBNetBinding.FormattingStrategy
indentation.PopOrDefault();
}
void Indent(TextArea textArea, Stack<string> indentation)
void Indent(ITextEditor editor, Stack<string> indentation)
{
bool useSpaces = textArea.TextEditorProperties.ConvertTabsToSpaces;
int indentationSize = textArea.TextEditorProperties.IndentationSize;
bool useSpaces = editor.Options.ConvertTabsToSpaces;
int indentationSize = editor.Options.IndentationSize;
string addIndent = (useSpaces) ? new string(' ', indentationSize) : "\t";
@ -769,18 +763,18 @@ namespace VBNetBinding.FormattingStrategy @@ -769,18 +763,18 @@ namespace VBNetBinding.FormattingStrategy
return false;
}
void ApplyToRange(TextArea textArea, Stack<string> indentation, int begin, int end, int selBegin, int selEnd)
void ApplyToRange(ITextEditor editor, Stack<string> indentation, int begin, int end, int selBegin, int selEnd)
{
bool multiLine = false;
for (int i = begin; i < end; i++) {
LineSegment curLine = textArea.Document.GetLineSegment(i);
string lineText = textArea.Document.GetText(curLine).Trim(' ', '\t', '\r', '\n');
for (int i = begin; i <= end; i++) {
IDocumentLine curLine = editor.Document.GetLine(i);
string lineText = curLine.Text.Trim(' ', '\t', '\r', '\n');
string noComments = StripComment(lineText).TrimEnd(' ', '\t', '\r', '\n');
if (i < selBegin || i > selEnd) {
indentation.PopOrDefault();
indentation.Push(GetIndentation(textArea, i));
indentation.Push(DocumentUtilitites.GetIndentation(editor.Document, curLine.Offset));
}
// change indentation before (indent this line)
@ -789,11 +783,11 @@ namespace VBNetBinding.FormattingStrategy @@ -789,11 +783,11 @@ namespace VBNetBinding.FormattingStrategy
multiLine = false;
}
SmartReplaceLine(textArea.Document, curLine, (indentation.PeekOrDefault() ?? string.Empty) + lineText);
editor.Document.SmartReplaceLine(curLine, (indentation.PeekOrDefault() ?? string.Empty) + lineText);
// change indentation afterwards (indent next line)
if (!multiLine && noComments.EndsWith("_")) {
Indent(textArea, indentation);
Indent(editor, indentation);
multiLine = true;
}
@ -813,25 +807,18 @@ namespace VBNetBinding.FormattingStrategy @@ -813,25 +807,18 @@ namespace VBNetBinding.FormattingStrategy
return false;
}
protected override int SmartIndentLine(TextArea textArea, int line)
{
if (line <= 0)
return AutoIndentLine(textArea, line);
return SmartIndentInternal(textArea, line, line);
}
/// <summary>
/// Gets the next member after the specified caret position.
/// </summary>
object GetMemberAfter(TextArea textArea, int caretLine)
object GetMemberAfter(ITextEditor editor, int caretLine)
{
string fileName = textArea.MotherTextEditorControl.FileName;
string fileName = editor.FileName;
object nextElement = null;
if (fileName != null && fileName.Length > 0 ) {
ParseInformation parseInfo = ParserService.ParseFile(fileName, textArea.Document.TextContent);
ParseInformation parseInfo = ParserService.ParseFile(fileName, editor.Document);
if (parseInfo != null) {
ICompilationUnit currentCompilationUnit = parseInfo.BestCompilationUnit;
ICompilationUnit currentCompilationUnit = parseInfo.CompilationUnit;
if (currentCompilationUnit != null) {
IClass currentClass = currentCompilationUnit.GetInnermostClass(caretLine, 0);
int nextElementLine = int.MaxValue;
@ -849,25 +836,7 @@ namespace VBNetBinding.FormattingStrategy @@ -849,25 +836,7 @@ namespace VBNetBinding.FormattingStrategy
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) {
foreach (IMember m in currentClass.AllMembers) {
if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) {
nextElementLine = m.Region.BeginLine;
nextElement = m;
@ -880,7 +849,13 @@ namespace VBNetBinding.FormattingStrategy @@ -880,7 +849,13 @@ namespace VBNetBinding.FormattingStrategy
return nextElement;
}
public override void IndentLine(ITextEditor editor, IDocumentLine line)
{
base.IndentLine(editor, line);
}
#region SearchBracket
/*
public override int SearchBracketBackward(IDocument document, int offset, char openBracket, char closingBracket)
{
bool inString = false;
@ -955,8 +930,7 @@ namespace VBNetBinding.FormattingStrategy @@ -955,8 +930,7 @@ namespace VBNetBinding.FormattingStrategy
}
}
return -1;
}
}*/
#endregion
}
*/
}

2
src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBStatement.cs

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
// </file>
using System;
namespace VBNetBinding.FormattingStrategy
namespace VBNetBinding
{
public class VBStatement
{

23
src/AddIns/BackendBindings/VBNetBinding/Project/Src/VBNetLanguageBinding.cs

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Siegfried Pammer" email="siegfriedpammer@gmail.com"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.SharpDevelop.Editor;
using System;
using ICSharpCode.SharpDevelop;
namespace VBNetBinding
{
/// <summary>
/// Description of VBNetLanguageBinding.
/// </summary>
public class VBNetLanguageBinding : DefaultLanguageBinding
{
public override IFormattingStrategy FormattingStrategy {
get { return new VBNetFormattingStrategy(); }
}
}
}

6
src/AddIns/BackendBindings/VBNetBinding/Project/VBNetBinding.addin

@ -98,9 +98,9 @@ @@ -98,9 +98,9 @@
<Path name = "/AddIns/DefaultTextEditor/CodeCompletion">
<CodeCompletionBinding id = "VBNet" extensions = ".vb" class = "VBNetBinding.VBNetCompletionBinding"/>
</Path>
<Path name = "/AddIns/DefaultTextEditor/Formatter/VBNET">
<Class id ="VBNetFormatter" class = "VBNetBinding.FormattingStrategy.VBFormattingStrategy"/>
<Path name="/SharpDevelop/Workbench/LanguageBindings">
<LanguageBinding id="VBNet" class="VBNetBinding.VBNetLanguageBinding" extensions=".vb" />
</Path>
<Path name = "/Workspace/Icons">

1
src/AddIns/BackendBindings/VBNetBinding/Project/VBNetBinding.csproj

@ -51,6 +51,7 @@ @@ -51,6 +51,7 @@
<EmbeddedResource Include="Resources\BuildOptions.xfrm" />
<Compile Include="Src\Extensions.cs" />
<Compile Include="Src\FormattingStrategy\VBStatement.cs" />
<Compile Include="Src\VBNetLanguageBinding.cs" />
<Compile Include="Src\VBNetProjectBinding.cs" />
<Compile Include="Src\FormattingStrategy\VBNetFormattingStrategy.cs" />
<Compile Include="Src\OptionPanels\BuildOptions.cs">

424
src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndOperatorTests.cs

@ -5,17 +5,14 @@ @@ -5,17 +5,14 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
using System;
using NUnit.Framework;
using ICSharpCode.AvalonEdit;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop;
using ICSharpCode.TextEditor;
using VBNetBinding;
using VBNetBinding.FormattingStrategy;
using NUnit.Framework;
namespace VBNetBinding.Tests
{
/*
/// <summary>
/// Tests that Operator overrides have "End Operator" added after the user presses the return key.
/// </summary>
@ -34,295 +31,358 @@ namespace VBNetBinding.Tests @@ -34,295 +31,358 @@ namespace VBNetBinding.Tests
/// Checks that when the user presses the return key after the Operator line that the
/// expected code is generated.
/// </summary>
void RunFormatLineTest(string code, string expectedCode)
void RunFormatLineTest(string code, string expectedCode, int expectedOffset)
{
string foo = "As Foo";
string foo = "As Foo\r\n";
int cursorOffset = code.IndexOf(foo) + foo.Length;
int line = 2;
using (TextEditorControl editor = new TextEditorControl()) {
editor.Document.TextContent = code;
editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursorOffset);
VBFormattingStrategy formattingStrategy = new VBFormattingStrategy();
formattingStrategy.FormatLine(editor.ActiveTextAreaControl.TextArea, line, cursorOffset, '\n');
Assert.AreEqual(expectedCode, editor.Document.TextContent);
}
AvalonEditTextEditorAdapter editor = new AvalonEditTextEditorAdapter(new TextEditor());
editor.Document.Text = code;
editor.Caret.Offset = cursorOffset;
VBNetFormattingStrategy formattingStrategy = new VBNetFormattingStrategy();
formattingStrategy.FormatLine(editor, '\n');
Assert.AreEqual(expectedCode, editor.Document.Text);
Assert.AreEqual(expectedOffset, editor.Caret.Offset);
}
[Test]
public void AdditionOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void AdditionOperatorWithExistingEndOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"\tEnd Operator\r\n" +
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void ExistingEndOperatorHasWhitespaceInbetween()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"\tEnd Operator\r\n" +
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
RunFormatLineTest(code, expectedCode);
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator +(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void AdditionOperatorHasWhitespaceAfterOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator + (ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator + (ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator + (ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator + (ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator + (ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void AdditionOperatorWithNoWhitespace()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator+(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator+(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator+(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator+(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator+(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void SubtractionOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator -(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator -(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator -(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator -(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator -(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void IsTrueOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator IsTrue(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator IsTrue(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator IsTrue(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator IsTrue(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
RunFormatLineTest(code, expectedCode);
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator IsTrue(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void MultiplicationOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator *(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator *(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator *(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator *(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator *(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void DivisionOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator /(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator /(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator /(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator /(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator /(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void IntegerDivisionOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator \\(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator \\(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator \\(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator \\(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
RunFormatLineTest(code, expectedCode);
}
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator \\(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void StringConcatenationOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator &(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator &(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator &(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator &(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
RunFormatLineTest(code, expectedCode);
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator &(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void ExponentationcationOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator ^(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator ^(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator ^(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator ^(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator ^(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
}
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void EqualityOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator =(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator =(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator =(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator =(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator =(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
}
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void GreaterThanOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator >(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator >(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator >(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator >(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator >(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
}
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void LessThanOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator <(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator <(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator <(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator <(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator <(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
}
RunFormatLineTest(code, expectedCode, expectedOffset);
}
[Test]
public void InequalityOperator()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared Operator <>(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared Operator <>(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared Operator <>(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
"\tPublic Shared Operator <>(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t\r\n" +
"\tEnd Operator\r\n" +
"End Class";
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared Operator <>(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\t").Length;
RunFormatLineTest(code, expectedCode);
}
RunFormatLineTest(code, expectedCode, expectedOffset);
}
/// <summary>
/// Check that a method that starts with "Operator" is ignored.
@ -331,18 +391,20 @@ namespace VBNetBinding.Tests @@ -331,18 +391,20 @@ namespace VBNetBinding.Tests
public void MethodStartsWithOperatorString()
{
string code = "Public Class Foo\r\n" +
"\tPublic Shared OperatorIgnore(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
"\tPublic Shared OperatorIgnore(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Shared OperatorIgnore(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\r\n" +
"End Class";
"\tPublic Shared OperatorIgnore(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t\r\n" +
"End Class";
RunFormatLineTest(code, expectedCode);
}
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Shared OperatorIgnore(ByVal lhs As Foo, ByVal rhs As Foo) As Foo\r\n" +
"\t").Length;
RunFormatLineTest(code, expectedCode, expectedOffset);
}
}
*/
}

73
src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndSubTests.cs

@ -6,16 +6,13 @@ @@ -6,16 +6,13 @@
// </file>
using System;
using NUnit.Framework;
using ICSharpCode.AvalonEdit;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop;
using ICSharpCode.TextEditor;
using VBNetBinding;
using VBNetBinding.FormattingStrategy;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
using NUnit.Framework;
namespace VBNetBinding.Tests
{
/*
/// <summary>
/// Tests that Operator overrides have "End Operator" added after the user presses the return key.
/// </summary>
@ -38,9 +35,8 @@ namespace VBNetBinding.Tests @@ -38,9 +35,8 @@ namespace VBNetBinding.Tests
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Class";
string bar = "Bar";
string bar = "Bar\r\n";
int cursorOffset = code.IndexOf(bar) + bar.Length;
int line = 2;
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
@ -48,14 +44,23 @@ namespace VBNetBinding.Tests @@ -48,14 +44,23 @@ namespace VBNetBinding.Tests
"\tEnd Sub\r\n" +
"End Class";
using (TextEditorControl editor = new TextEditorControl()) {
editor.Document.TextContent = code;
editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursorOffset);
VBFormattingStrategy formattingStrategy = new VBFormattingStrategy();
formattingStrategy.FormatLine(editor.ActiveTextAreaControl.TextArea, line, cursorOffset, '\n');
Assert.AreEqual(expectedCode, editor.Document.TextContent);
}
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t\t").Length;
RunTest(code, cursorOffset, expectedCode, expectedOffset, '\n');
}
void RunTest(string code, int cursorOffset, string expectedCode, int expectedOffset, char keyPressed)
{
AvalonEditTextEditorAdapter editor = new AvalonEditTextEditorAdapter(new TextEditor());
editor.Document.Text = code;
editor.Caret.Offset = cursorOffset;
VBNetFormattingStrategy formattingStrategy = new VBNetFormattingStrategy();
formattingStrategy.FormatLine(editor, keyPressed);
Assert.AreEqual(expectedCode, editor.Document.Text);
Assert.AreEqual(expectedOffset, editor.Caret.Offset);
}
[Test]
@ -68,9 +73,8 @@ namespace VBNetBinding.Tests @@ -68,9 +73,8 @@ namespace VBNetBinding.Tests
"\tEnd Sub\r\n" +
"End Class";
string bar = "Then";
string bar = "Then\r\n";
int cursorOffset = code.IndexOf(bar) + bar.Length;
int line = 3;
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
@ -80,14 +84,12 @@ namespace VBNetBinding.Tests @@ -80,14 +84,12 @@ namespace VBNetBinding.Tests
"\tEnd Sub\r\n" +
"End Class";
using (TextEditorControl editor = new TextEditorControl()) {
editor.Document.TextContent = code;
editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursorOffset);
VBFormattingStrategy formattingStrategy = new VBFormattingStrategy();
formattingStrategy.FormatLine(editor.ActiveTextAreaControl.TextArea, line, cursorOffset, '\n');
Assert.AreEqual(expectedCode, editor.Document.TextContent);
}
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t\tIf True Then\r\n" +
"\t\t\t").Length;
RunTest(code, cursorOffset, expectedCode, expectedOffset, '\n');
}
[Test]
@ -100,9 +102,8 @@ namespace VBNetBinding.Tests @@ -100,9 +102,8 @@ namespace VBNetBinding.Tests
"\tEnd Sub\r\n" +
"End Class";
string bar = "Then _";
string bar = "Then _\r\n";
int cursorOffset = code.IndexOf(bar) + bar.Length;
int line = 3;
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
@ -111,14 +112,12 @@ namespace VBNetBinding.Tests @@ -111,14 +112,12 @@ namespace VBNetBinding.Tests
"\tEnd Sub\r\n" +
"End Class";
using (TextEditorControl editor = new TextEditorControl()) {
editor.Document.TextContent = code;
editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursorOffset);
VBFormattingStrategy formattingStrategy = new VBFormattingStrategy();
formattingStrategy.FormatLine(editor.ActiveTextAreaControl.TextArea, line, cursorOffset, '\n');
Assert.AreEqual(expectedCode, editor.Document.TextContent);
}
int expectedOffset = ("Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t\tIf True Then _\r\n" +
"\t\t\t").Length;
RunTest(code, cursorOffset, expectedCode, expectedOffset, '\n');
}
}*/
}
}

40
src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs

@ -5,15 +5,14 @@ @@ -5,15 +5,14 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.TextEditor;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
using System;
using ICSharpCode.AvalonEdit;
using ICSharpCode.Core;
using NUnit.Framework;
using VBNetBinding.FormattingStrategy;
namespace VBNetBinding.Tests.FormattingStrategy
namespace VBNetBinding.Tests
{
/*
[TestFixture]
public class IndentationTests
{
@ -33,6 +32,24 @@ End Interface"; @@ -33,6 +32,24 @@ End Interface";
RunFormatTest(code, expectedCode);
}
[Test]
public void InterfaceWithNewLineAtEndTest()
{
string code = @"Interface t
Sub Test()
Sub Test2()
End Interface";
string expectedCode = @"Interface t
Sub Test()
Sub Test2()
End Interface";
RunFormatTest(code, expectedCode);
}
[Test]
public void ArrayInitializerTest()
{
@ -119,13 +136,12 @@ End Class"; @@ -119,13 +136,12 @@ End Class";
void RunFormatTest(string code, string expectedCode)
{
using (TextEditorControl editor = new TextEditorControl()) {
editor.Document.TextContent = code;
VBFormattingStrategy formattingStrategy = new VBFormattingStrategy();
formattingStrategy.IndentLines(editor.ActiveTextAreaControl.TextArea, 0, editor.Document.TotalNumberOfLines);
Assert.AreEqual(expectedCode, editor.Document.TextContent);
}
AvalonEditTextEditorAdapter editor = new AvalonEditTextEditorAdapter(new TextEditor());
editor.Document.Text = code;
VBNetFormattingStrategy formattingStrategy = new VBNetFormattingStrategy();
formattingStrategy.IndentLines(editor, 0, editor.Document.TotalNumberOfLines);
Assert.AreEqual(expectedCode, editor.Document.Text);
}
[TestFixtureSetUp]
@ -135,5 +151,5 @@ End Class"; @@ -135,5 +151,5 @@ End Class";
PropertyService.InitializeService(String.Empty, String.Empty, "VBNetBindingTests");
}
}
}*/
}
}

34
src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/InterfaceTests.cs

@ -4,17 +4,14 @@ @@ -4,17 +4,14 @@
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <version>$Revision: 3548 $</version>
// </file>
using ICSharpCode.AvalonEdit;
using System;
using NUnit.Framework;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop;
using ICSharpCode.TextEditor;
using VBNetBinding;
using VBNetBinding.FormattingStrategy;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
using NUnit.Framework;
namespace VBNetBinding.Tests
{
/*
/// <summary>
/// Tests the special case of an interface. for ex. no insertion of End-Tags etc.
/// </summary>
@ -37,23 +34,26 @@ namespace VBNetBinding.Tests @@ -37,23 +34,26 @@ namespace VBNetBinding.Tests
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"End Interface";
string bar = "Sub Bar";
string bar = "Sub Bar\r\n";
int cursorOffset = code.IndexOf(bar) + bar.Length;
int line = 2;
string expectedCode = "Public Interface Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t\r\n" +
"End Interface";
using (TextEditorControl editor = new TextEditorControl()) {
editor.Document.TextContent = code;
editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursorOffset);
VBFormattingStrategy formattingStrategy = new VBFormattingStrategy();
formattingStrategy.FormatLine(editor.ActiveTextAreaControl.TextArea, line, cursorOffset, '\n');
Assert.AreEqual(expectedCode, editor.Document.TextContent);
}
int expectedOffset = ("Public Interface Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t").Length;
AvalonEditTextEditorAdapter editor = new AvalonEditTextEditorAdapter(new TextEditor());
editor.Document.Text = code;
editor.Caret.Offset = cursorOffset;
VBNetFormattingStrategy formattingStrategy = new VBNetFormattingStrategy();
formattingStrategy.FormatLine(editor, '\n');
Assert.AreEqual(expectedCode, editor.Document.Text);
Assert.AreEqual(expectedOffset, editor.Caret.Offset);
}
}*/
}
}

19
src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj

@ -12,7 +12,7 @@ @@ -12,7 +12,7 @@
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<FileAlignment>4096</FileAlignment>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
@ -31,20 +31,27 @@ @@ -31,20 +31,27 @@
<DebugType>None</DebugType>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="PresentationFramework">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="nunit.framework">
<HintPath>..\..\..\..\Tools\NUnit\nunit.framework.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="WindowsBase">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Libraries\ICSharpCode.TextEditor\Project\ICSharpCode.TextEditor.csproj">
<Project>{2D18BE89-D210-49EB-A9DD-2246FBB3DF6D}</Project>
<Name>ICSharpCode.TextEditor</Name>
<ProjectReference Include="..\..\..\..\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\ICSharpCode.AvalonEdit.csproj">
<Project>{6C55B776-26D4-4DB3-A6AB-87E783B2F3D1}</Project>
<Name>ICSharpCode.AvalonEdit</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Main\Core\Project\ICSharpCode.Core.csproj">
<Project>{35CEF10F-2D4C-45F2-9DD1-161E0FEC583C}</Project>

Loading…
Cancel
Save