Module: Spacer::CSharp

Defined in:
lib/spacer/csharp.rb

Class Method Summary collapse

Class Method Details

.count_bol_spaces_and_tabs(lines) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spacer/csharp.rb', line 5

def self.count_bol_spaces_and_tabs(lines)
  bol = OpenStruct.new
  bol.tabs = 0
  bol.spaces = 0
  in_multi_line_string = false

  for line in lines do
    in_bol = true
    i = 0
    while i < line.length do
      c = line[i]
      c1 = i < line.length - 1 ? line[i + 1] : "\0"

      if in_multi_line_string and c == "\"" and c1 != "\""
        in_multi_line_string = false
      elsif c == "@" and c1 == "\""
        in_multi_line_string = true
        i += 1
      elsif in_bol and !in_multi_line_string and c == " "
        bol.spaces += 1
      elsif in_bol and !in_multi_line_string and c == "\t"
        bol.tabs += 1
      else
        in_bol = false
      end
      i += 1
    end
  end

  bol
end

.tabify(lines, tabsize, round_down_spaces) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/spacer/csharp.rb', line 85

def self.tabify(lines, tabsize, round_down_spaces)
  # Insert tabs for spaces, but only at the beginning of lines and not inside @"..." or "..." strings
  in_multi_line_string = false
  i = 0

  while i < lines.length do
    line = lines[i]
    in_string = false
    bol = true
    num_bol_spaces = 0
    new_line = ""
    j = 0

    while j < line.length do
      c_1 = j > 0 ? line[j - 1] : "\0"
      c = line[j]
      c1 = j < line.length - 1 ? line[j + 1] : "\0"

      if !in_string and !in_multi_line_string and bol and c == " "
        # Just count the spaces
        num_bol_spaces += 1
      elsif !in_string and !in_multi_line_string and bol and c != " "
        bol = false

        new_line += "\t" * (num_bol_spaces / tabsize)

        if !round_down_spaces
          new_line += " " * (num_bol_spaces % tabsize)
        end
        # Process this character again as not BOL
        j -= 1
      elsif !in_multi_line_string and !in_string and c == '"'
        in_string = true
        new_line += c
      elsif !in_multi_line_string and !in_string and c == "@" and c1 == "\""
        in_multi_line_string = true
        new_line += c
        j += 1
        new_line += c1
      elsif in_string and c == "\"" and c_1 != "\\"
        in_string = false
        new_line += c
      elsif in_multi_line_string and c == "\"" and c1 != "\""
        in_multi_line_string = false
        new_line += c
      else
        new_line += c
      end

      lines[i] = new_line
      j += 1
    end
    i += 1
  end
end

.untabify(lines, tabsize) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/spacer/csharp.rb', line 37

def self.untabify(lines, tabsize)
  # Expand tabs anywhere on a line, but not inside @"..." strings
  in_multi_line_string = false

  i = 0
  while i < lines.length do
    line = lines[i]
    in_string = false
    new_line = ""
    j = 0

    while j < line.length do
      c_1 = j > 0 ? line[j - 1] : '\0'
      c = line[j]
      c1 = j < line.length - 1 ? line[j + 1] : '\0'

      raise "line #{i + 1} has overlapping regular and multiline strings" if (in_string and in_multi_line_string)

      if !in_multi_line_string and c == "\t"
        # Add spaces to next tabstop
        num_spaces = tabsize - (new_line.length % tabsize)

        new_line += " " * num_spaces
      elsif !in_multi_line_string and !in_string and c == "\""
        in_string = true
        new_line += c
      elsif !in_multi_line_string and !in_string and c == "@" and c1 == "\""
        in_multi_line_string = true
        new_line += c
        j += 1
        new_line += c1
      elsif in_string and c == "\"" and c_1 != "\\"
        in_string = false
        new_line += c
      elsif in_multi_line_string and c == "\"" and c1 != "\""
        in_multi_line_string = false
        new_line += c
      else
        new_line += c
      end

      lines[i] = new_line
      j += 1
    end
    i += 1
  end
end