/AvalonEdit/ICSharpCode.AvalonEdit/Indentation/CSharp/CSharpIndentationStrategy.cs

http://github.com/icsharpcode/ILSpy · C# · 95 lines · 48 code · 10 blank · 37 comment · 5 complexity · 23df775d3a39d98cbfb847bf200abdc6 MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using ICSharpCode.AvalonEdit.Document;
  20. namespace ICSharpCode.AvalonEdit.Indentation.CSharp
  21. {
  22. /// <summary>
  23. /// Smart indentation for C#.
  24. /// </summary>
  25. public class CSharpIndentationStrategy : DefaultIndentationStrategy
  26. {
  27. /// <summary>
  28. /// Creates a new CSharpIndentationStrategy.
  29. /// </summary>
  30. public CSharpIndentationStrategy()
  31. {
  32. }
  33. /// <summary>
  34. /// Creates a new CSharpIndentationStrategy and initializes the settings using the text editor options.
  35. /// </summary>
  36. public CSharpIndentationStrategy(TextEditorOptions options)
  37. {
  38. this.IndentationString = options.IndentationString;
  39. }
  40. string indentationString = "\t";
  41. /// <summary>
  42. /// Gets/Sets the indentation string.
  43. /// </summary>
  44. public string IndentationString {
  45. get { return indentationString; }
  46. set {
  47. if (string.IsNullOrEmpty(value))
  48. throw new ArgumentException("Indentation string must not be null or empty");
  49. indentationString = value;
  50. }
  51. }
  52. /// <summary>
  53. /// Performs indentation using the specified document accessor.
  54. /// </summary>
  55. /// <param name="document">Object used for accessing the document line-by-line</param>
  56. /// <param name="keepEmptyLines">Specifies whether empty lines should be kept</param>
  57. public void Indent(IDocumentAccessor document, bool keepEmptyLines)
  58. {
  59. if (document == null)
  60. throw new ArgumentNullException("document");
  61. IndentationSettings settings = new IndentationSettings();
  62. settings.IndentString = this.IndentationString;
  63. settings.LeaveEmptyLines = keepEmptyLines;
  64. IndentationReformatter r = new IndentationReformatter();
  65. r.Reformat(document, settings);
  66. }
  67. /// <inheritdoc cref="IIndentationStrategy.IndentLine"/>
  68. public override void IndentLine(TextDocument document, DocumentLine line)
  69. {
  70. int lineNr = line.LineNumber;
  71. TextDocumentAccessor acc = new TextDocumentAccessor(document, lineNr, lineNr);
  72. Indent(acc, false);
  73. string t = acc.Text;
  74. if (t.Length == 0) {
  75. // use AutoIndentation for new lines in comments / verbatim strings.
  76. base.IndentLine(document, line);
  77. }
  78. }
  79. /// <inheritdoc cref="IIndentationStrategy.IndentLines"/>
  80. public override void IndentLines(TextDocument document, int beginLine, int endLine)
  81. {
  82. Indent(new TextDocumentAccessor(document, beginLine, endLine), true);
  83. }
  84. }
  85. }