Browse Source

TextReplace actions can now depend on each other.

newNRvisualizers
mike 14 years ago
parent
commit
1503054b29
  1. 30
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 13
      ICSharpCode.NRefactory.CSharp/Refactoring/TextReplaceAction.cs

30
ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs

@ -817,6 +817,9 @@ namespace ICSharpCode.NRefactory.CSharp
return; return;
int originalLevel = curIndent.Level; int originalLevel = curIndent.Level;
bool isBlock = node is BlockStatement; bool isBlock = node is BlockStatement;
TextReplaceAction beginBraceAction = null;
TextReplaceAction endBraceAction = null;
switch (braceForcement) { switch (braceForcement) {
case BraceForcement.DoNotChange: case BraceForcement.DoNotChange:
//nothing //nothing
@ -846,7 +849,7 @@ namespace ICSharpCode.NRefactory.CSharp
} }
if (IsLineIsEmptyUpToEol (document.GetOffset (node.StartLocation))) if (IsLineIsEmptyUpToEol (document.GetOffset (node.StartLocation)))
startBrace += this.EolMarker + GetIndentation (node.StartLocation.Line); startBrace += this.EolMarker + GetIndentation (node.StartLocation.Line);
AddChange (start, offset - start, startBrace); beginBraceAction = AddChange (start, offset - start, startBrace);
} }
break; break;
case BraceForcement.RemoveBraces: case BraceForcement.RemoveBraces:
@ -859,8 +862,8 @@ namespace ICSharpCode.NRefactory.CSharp
int offset2 = document.GetOffset (node.EndLocation); int offset2 = document.GetOffset (node.EndLocation);
int end = SearchWhitespaceStart (offset2 - 1); int end = SearchWhitespaceStart (offset2 - 1);
AddChange (start, offset1 - start + 1, null); beginBraceAction = AddChange (start, offset1 - start + 1, null);
AddChange (end + 1, offset2 - end, null); endBraceAction = AddChange (end + 1, offset2 - end, null);
node = block.FirstChild; node = block.FirstChild;
isBlock = false; isBlock = false;
} }
@ -921,10 +924,14 @@ namespace ICSharpCode.NRefactory.CSharp
break; break;
} }
if (startBrace != null) if (startBrace != null)
AddChange (offset, 0, startBrace); endBraceAction = AddChange (offset, 0, startBrace);
} }
break; break;
} }
if (beginBraceAction != null && endBraceAction != null) {
beginBraceAction.DependsOn = endBraceAction;
endBraceAction.DependsOn = beginBraceAction;
}
} }
void EnforceBraceStyle (BraceStyle braceStyle, AstNode lbrace, AstNode rbrace) void EnforceBraceStyle (BraceStyle braceStyle, AstNode lbrace, AstNode rbrace)
@ -1000,14 +1007,14 @@ namespace ICSharpCode.NRefactory.CSharp
AddChange (whitespaceEnd, rbraceOffset - whitespaceEnd, endIndent); AddChange (whitespaceEnd, rbraceOffset - whitespaceEnd, endIndent);
} }
void AddChange (int offset, int removedChars, string insertedText) TextReplaceAction AddChange (int offset, int removedChars, string insertedText)
{ {
if (changes.Any (c => c.Offset == offset && c.RemovedChars == removedChars if (changes.Any (c => c.Offset == offset && c.RemovedChars == removedChars
&& c.InsertedText == insertedText)) && c.InsertedText == insertedText))
return; return null;
string currentText = document.GetText (offset, removedChars); string currentText = document.GetText (offset, removedChars);
if (currentText == insertedText) if (currentText == insertedText)
return; return null;
if (currentText.Any (c => !(char.IsWhiteSpace (c) || c == '\r' || c == '\t' || c == '{' || c == '}'))) if (currentText.Any (c => !(char.IsWhiteSpace (c) || c == '\r' || c == '\t' || c == '{' || c == '}')))
throw new InvalidOperationException ("Tried to remove non ws chars: '" + currentText + "'"); throw new InvalidOperationException ("Tried to remove non ws chars: '" + currentText + "'");
foreach (var change in changes) { foreach (var change in changes) {
@ -1015,7 +1022,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (removedChars > 0 && insertedText == change.InsertedText) { if (removedChars > 0 && insertedText == change.InsertedText) {
change.RemovedChars = removedChars; change.RemovedChars = removedChars;
// change.InsertedText = insertedText; // change.InsertedText = insertedText;
return; return null;
} }
if (!string.IsNullOrEmpty (change.InsertedText)) { if (!string.IsNullOrEmpty (change.InsertedText)) {
change.InsertedText += insertedText; change.InsertedText += insertedText;
@ -1023,13 +1030,14 @@ namespace ICSharpCode.NRefactory.CSharp
change.InsertedText = insertedText; change.InsertedText = insertedText;
} }
change.RemovedChars = System.Math.Max (removedChars, change.RemovedChars); change.RemovedChars = System.Math.Max (removedChars, change.RemovedChars);
return; return null;
} }
} }
//Console.WriteLine ("offset={0}, removedChars={1}, insertedText={2}, removedText={3}", offset, removedChars, insertedText == null ? "<null>" : insertedText.Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t").Replace (" ", "."), removedChars > 0 ? document.GetText (offset, removedChars).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t").Replace (" ", ".") : "null"); //Console.WriteLine ("offset={0}, removedChars={1}, insertedText={2}, removedText={3}", offset, removedChars, insertedText == null ? "<null>" : insertedText.Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t").Replace (" ", "."), removedChars > 0 ? document.GetText (offset, removedChars).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t").Replace (" ", ".") : "null");
//Console.WriteLine (Environment.StackTrace); //Console.WriteLine (Environment.StackTrace);
var result = factory.CreateTextReplaceAction (offset, removedChars, insertedText);
changes.Add (factory.CreateTextReplaceAction (offset, removedChars, insertedText)); changes.Add (result);
return result;
} }
public bool IsLineIsEmptyUpToEol (TextLocation startLocation) public bool IsLineIsEmptyUpToEol (TextLocation startLocation)

13
ICSharpCode.NRefactory.CSharp/Refactoring/TextReplaceAction.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;
namespace ICSharpCode.NRefactory.CSharp.Refactoring namespace ICSharpCode.NRefactory.CSharp.Refactoring
@ -43,7 +42,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
get; get;
set; set;
} }
/// <summary>
/// Gets or sets the text replace action where this action depends on.
/// For example inserting a '{' ... '}' pair would require to insert the '}' when '{' is inserted.
/// Therefore the both actions depend on each other.
/// </summary>
public TextReplaceAction DependsOn {
get;
set;
}
int removedChars; int removedChars;
/// <summary> /// <summary>
/// Gets or sets the numer of chars to removed. /// Gets or sets the numer of chars to removed.

Loading…
Cancel
Save