Browse Source

Improved smart indentation a bit.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1731 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
3c5cd69aff
  1. 10
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs
  2. 74
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/DocumentAccessor.cs
  3. 20
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/Indentation.cs

10
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

@ -391,15 +391,13 @@ namespace CSharpBinding.FormattingStrategy @@ -391,15 +391,13 @@ namespace CSharpBinding.FormattingStrategy
case ':':
case ')':
case ']':
case '}':
case '{':
return textArea.Document.FormattingStrategy.IndentLine(textArea, lineNr);
case '\n':
if (lineNr <= 0) {
return IndentLine(textArea, lineNr);
if (textArea.Document.TextEditorProperties.IndentStyle == IndentStyle.Smart) {
return textArea.Document.FormattingStrategy.IndentLine(textArea, lineNr);
}
break;
case '\n':
string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove);
//// curLine might have some text which should be added to indentation
curLineText = "";

74
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/DocumentAccessor.cs

@ -33,7 +33,7 @@ namespace CSharpBinding.FormattingStrategy @@ -33,7 +33,7 @@ namespace CSharpBinding.FormattingStrategy
}
#region DocumentAccessor
public class DocumentAccessor : IDocumentAccessor
public sealed class DocumentAccessor : IDocumentAccessor
{
IDocument doc;
@ -113,7 +113,7 @@ namespace CSharpBinding.FormattingStrategy @@ -113,7 +113,7 @@ namespace CSharpBinding.FormattingStrategy
#endregion
#region FileAccessor
public class FileAccessor : IDisposable, IDocumentAccessor
public sealed class FileAccessor : IDisposable, IDocumentAccessor
{
public bool Dirty {
get {
@ -132,16 +132,12 @@ namespace CSharpBinding.FormattingStrategy @@ -132,16 +132,12 @@ namespace CSharpBinding.FormattingStrategy
ArrayList lines = new ArrayList();
bool dirty = false;
System.Text.Encoding encoding;
string filename;
public FileAccessor(string filename)
{
this.filename = filename;
f = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None);
// TODO: Auto-detect encoding
encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
r = new StreamReader(f, encoding);
f = new FileStream(filename, FileMode.Open, FileAccess.Read);
r = ICSharpCode.TextEditor.Util.FileReader.OpenStream(f, ICSharpCode.Core.ParserService.DefaultFileEncoding, ICSharpCode.Core.ParserService.DefaultFileEncoding);
}
int num = 0;
@ -180,6 +176,7 @@ namespace CSharpBinding.FormattingStrategy @@ -180,6 +176,7 @@ namespace CSharpBinding.FormattingStrategy
public void Close()
{
System.Text.Encoding encoding = r.CurrentEncoding;
r.Close();
f.Close();
if (dirty) {
@ -194,4 +191,65 @@ namespace CSharpBinding.FormattingStrategy @@ -194,4 +191,65 @@ namespace CSharpBinding.FormattingStrategy
}
}
#endregion
#region StringAccessor
public sealed class StringAccessor : IDocumentAccessor
{
public bool Dirty {
get {
return dirty;
}
}
public bool ReadOnly {
get {
return false;
}
}
StringReader r;
StringWriter w;
bool dirty = false;
public string CodeOutput {
get {
return w.ToString();
}
}
public StringAccessor(string code)
{
r = new StringReader(code);
w = new StringWriter();
}
int num = 0;
public int LineNumber {
get { return num; }
}
string text = "";
public string Text {
get {
return text;
}
set {
dirty = true;
text = value;
}
}
public bool Next()
{
if (num > 0) {
w.WriteLine(text);
}
text = r.ReadLine();
++num;
return text != null;
}
}
#endregion
}

20
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/Indentation.cs

@ -6,19 +6,19 @@ @@ -6,19 +6,19 @@
// </file>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace CSharpBinding.FormattingStrategy
{
public class IndentationSettings
public sealed class IndentationSettings
{
public string IndentString = "\t";
/// <summary>Leave empty lines empty.</summary>
public bool LeaveEmptyLines = true;
}
public class IndentationReformatter
public sealed class IndentationReformatter
{
public struct Block
{
@ -46,7 +46,7 @@ namespace CSharpBinding.FormattingStrategy @@ -46,7 +46,7 @@ namespace CSharpBinding.FormattingStrategy
}
StringBuilder wordBuilder;
Stack blocks; // blocks contains all blocks outside of the current
Stack<Block> blocks; // blocks contains all blocks outside of the current
Block block; // block is the current block
bool inString = false;
@ -71,7 +71,7 @@ namespace CSharpBinding.FormattingStrategy @@ -71,7 +71,7 @@ namespace CSharpBinding.FormattingStrategy
public void Init()
{
wordBuilder = new StringBuilder();
blocks = new Stack();
blocks = new Stack<Block>();
block = new Block();
block.InnerIndent = "";
block.OuterIndent = "";
@ -224,10 +224,10 @@ namespace CSharpBinding.FormattingStrategy @@ -224,10 +224,10 @@ namespace CSharpBinding.FormattingStrategy
case '}':
while (block.Bracket != '{') {
if (blocks.Count == 0) break;
block = (Block)blocks.Pop();
block = blocks.Pop();
}
if (blocks.Count == 0) break;
block = (Block)blocks.Pop();
block = blocks.Pop();
block.Continuation = false;
block.OneLineBlock = false;
break;
@ -241,13 +241,13 @@ namespace CSharpBinding.FormattingStrategy @@ -241,13 +241,13 @@ namespace CSharpBinding.FormattingStrategy
block.Indent(set,
(oldBlock.OneLineBlock ? set.IndentString : "") +
(oldBlock.Continuation ? set.IndentString : "") +
new String(' ', i + 1));
(i == line.Length - 1 ? set.IndentString : new String(' ', i + 1)));
block.Bracket = c;
break;
case ')':
if (blocks.Count == 0) break;
if (block.Bracket == '(') {
block = (Block)blocks.Pop();
block = blocks.Pop();
if (IsSingleStatementKeyword(block.LastWord))
block.Continuation = false;
}
@ -255,7 +255,7 @@ namespace CSharpBinding.FormattingStrategy @@ -255,7 +255,7 @@ namespace CSharpBinding.FormattingStrategy
case ']':
if (blocks.Count == 0) break;
if (block.Bracket == '[')
block = (Block)blocks.Pop();
block = blocks.Pop();
break;
case ';':
case ',':

Loading…
Cancel
Save