Module: EditorConfig

Defined in:
lib/editor_config.rb,
lib/editor_config/version.rb

Constant Summary collapse

CONFIG_FILENAME =

Public: Default config basename.

".editorconfig".freeze
INDENT_STYLE =
"indent_style".freeze
INDENT_SIZE =
"indent_size".freeze
TAB_WIDTH =
"tab_width".freeze
END_OF_LINE =
"end_of_line".freeze
CHARSET =
"charset".freeze
TRIM_TRAILING_WHITESPACE =
"trim_trailing_whitespace".freeze
INSERT_FINAL_NEWLINE =
"insert_final_newline".freeze
MAX_LINE_LENGTH =
"max_line_length".freeze
TRUE =
"true".freeze
FALSE =
"false".freeze
SPACE =
"space".freeze
TAB =
"tab".freeze
CR =
"cr".freeze
LF =
"lf".freeze
CRLF =
"crlf".freeze
LATIN1 =
"latin1".freeze
UTF_8 =
"utf-8".freeze
UTF_8_BOM =
"utf-8-bom".freeze
UTF_16BE =
"utf-16be".freeze
UTF_16LE =
"utf-16le".freeze
DEFAULT_FILENAME =

Internal: Default filename to use if path is too long or has too many components.

"filename".freeze
MAX_LINE =

Internal: Maximum number of bytes to read per line. Lines over this limit will be truncated.

200
MAX_SECTION_NAME =

Internal: Maximum byte length of section name Strings. Names over this limit will be truncated.

500
MAX_PROPERTY_NAME =

Internal: Maximum byte length of property name String. Names over this limit will be truncated.

500
MAX_FILENAME =

Internal: Maximum byte length of filename path. Paths over this limit will default to global “*” properties.

4096
MAX_FILENAME_COMPONENTS =

Internal: Maximum number of directories a filename can have. Paths this deep will default to global “*” properties.

25
FNMATCH_ESCAPED_LBRACE =

Internal: Temporary replacement constants used within fnmatch.

"FNMATCH-ESCAPED-LBRACE".freeze
FNMATCH_ESCAPED_RBRACE =
"FNMATCH-ESCAPED-RBRACE".freeze
VERSION =
"0.2.0"
SPEC_VERSION =
"0.9.1"

Class Method Summary collapse

Class Method Details

.fnmatch?(pattern, path) ⇒ Boolean

Public: Test shell pattern against a path.

Modeled after editorconfig/fnmatch.py.

https://github.com/editorconfig/editorconfig-core-py/blob/master/editorconfig/fnmatch.py

pattern - String shell pattern path - String pathname

Returns true if path matches pattern, otherwise false.

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/editor_config.rb', line 165

def self.fnmatch?(pattern, path)
  flags = File::FNM_PATHNAME | File::FNM_EXTGLOB
  pattern = pattern.dup

  pattern.gsub!(/(\{\w*\})/) {
    $1.gsub("{", FNMATCH_ESCAPED_LBRACE).gsub("}", FNMATCH_ESCAPED_RBRACE)
  }
  pattern.gsub!(/(\{[^}]+$)/) {
    $1.gsub("{", FNMATCH_ESCAPED_LBRACE)
  }
  pattern.gsub!(/^([^\{]+\})/) {
    $1.gsub("}", FNMATCH_ESCAPED_RBRACE)
  }

  pattern.gsub!(/(\{(.*)\})/) {
    bracket = $1
    inner = $2

    if inner =~ /^(\d+)\.\.(\d+)$/
      "{#{($1.to_i..$2.to_i).to_a.join(",")}}"
    elsif inner.include?(",")
      bracket
    else
      "#{FNMATCH_ESCAPED_LBRACE}#{inner}#{FNMATCH_ESCAPED_RBRACE}"
    end
  }

  pattern.gsub!(FNMATCH_ESCAPED_LBRACE, "\\{")
  pattern.gsub!(FNMATCH_ESCAPED_RBRACE, "\\}")

  pattern.gsub!(/\[(.*\/.*)\]/) {
    "\\[#{$1}\\]"
  }

  patterns = []

  # Expand "**" to match over path separators
  # TODO: Optimize the number of patterns we need
  patterns << pattern.gsub(/\/\*\*/, "/**/*")
  patterns << pattern.gsub(/\/\*\*/, "")
  patterns << pattern.gsub(/\*\*/, "**/*")
  patterns << pattern.gsub(/\*\*/, "/**/*")

  patterns.any? { |p| File.fnmatch?(p, path, flags) }
end

.load(path, config: CONFIG_FILENAME, version: SPEC_VERSION) ⇒ Object

Public: Load EditorConfig with a custom loader implementation.

path - String filename on file system config - Basename of config to search for (default: .editorconfig)

loader block

config_path - String "path/to/.editorconfig" to attempt to read from

Returns Hash of String properties and values.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/editor_config.rb', line 220

def self.load(path, config: CONFIG_FILENAME, version: SPEC_VERSION)
  hash = {}

  # Use default filename if path is too long
  path = DEFAULT_FILENAME if path.length > MAX_FILENAME

  components = traverse(path)

  # Use default filename if path has too many directories
  path = DEFAULT_FILENAME if components.length > MAX_FILENAME_COMPONENTS

  components.each do |subpath|
    config_path = subpath == "" ? config : "#{subpath}/#{config}"
    config_data = yield config_path
    next unless config_data

    sections, root = parse(config_data, version: version)
    section_properties = {}

    sections.each do |section, properties|
      if section.include?("/")
        section = section[1..-1] if section[0] == "/"
        pattern = subpath == "" ? section : "#{subpath}/#{section}"
      else
        pattern = "**/#{section}"
      end

      if fnmatch?(pattern, path)
        section_properties.merge!(properties)
      end
    end

    hash = section_properties.merge(hash)

    if root
      break
    end
  end

  hash
end

.load_file(*args) ⇒ Object

Public: Load EditorConfig for a specific file.

Starts at filename and walks up each directory gathering any .editorconfig files until it reaches a config marked as “root”.

path - String filename on file system config - Basename of config to search for (default: .editorconfig)

Returns Hash of String properties and values.



296
297
298
299
300
# File 'lib/editor_config.rb', line 296

def self.load_file(*args)
  EditorConfig.load(*args) do |path|
    File.read(path) if File.exist?(path)
  end
end

.parse(io, version: SPEC_VERSION) ⇒ Object

Public: Parse the contents of an .editorconfig.

io - An IO or String with the raw contents of an .editorconfig file.

Returns a tuple of a parsed Hash of information and a boolean flag if the file was marked as “root”. The hash contains string keys of each section of the config file.

An example hash would look like this: {

"*.rb" => {
  "indent_style" => "space",
  "indent_size" => "2",
  "charset" => "utf-8"
}

}



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/editor_config.rb', line 88

def self.parse(io, version: SPEC_VERSION)
  config, root = {}, false
  last_section = nil

  io.each_line do |line|
    line = line.sub(/\s+(;|#).+$/, "").chomp
    case line
    when /\Aroot(\s+)?\=(\s+)?true\Z/i
      root = true
    when /\A\s*\[(?<name>.+)\]\s*\Z/
      # section marker
      last_section = Regexp.last_match[:name][0, MAX_SECTION_NAME]
      config[last_section] ||= {}
    when /\A\s*(?<name>[[:word:]]+)\s*(\=|:)\s*(?<value>.+)\s*\Z/
      match = Regexp.last_match
      name, value = match[:name][0, MAX_PROPERTY_NAME].strip, match[:value].strip
      config[last_section][name] = value if last_section
    end
  end

  return config, root
end

.preprocess(config, version: SPEC_VERSION) ⇒ Object

Public: Normalize known universal properties.

config - Hash configuration version - String spec version

Returns new preprocessed Hash.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/editor_config.rb', line 117

def self.preprocess(config, version: SPEC_VERSION)
  config = config.reduce({}) { |h, (k, v)| h[k.downcase] = v; h }

  [
    INDENT_STYLE,
    INDENT_SIZE,
    TAB_WIDTH,
    END_OF_LINE,
    CHARSET,
    TRIM_TRAILING_WHITESPACE,
    INSERT_FINAL_NEWLINE,
    MAX_LINE_LENGTH
  ].each do |key|
    if config.key?(key)
      config[key] = config[key].downcase
    end
  end

  if !config.key?(TAB_WIDTH) && config.key?(INDENT_SIZE) && config[INDENT_SIZE] != TAB
    config[TAB_WIDTH] = config[INDENT_SIZE]
  end

  if version > "0.9"
    if !config.key?(INDENT_SIZE) && config[INDENT_STYLE] == TAB
      if config.key?(TAB_WIDTH)
        config[INDENT_SIZE] = config[TAB_WIDTH]
      else
        config[INDENT_SIZE] = TAB
      end
    end
  end

  config
end

.traverse(path) ⇒ Object

Internal: Generate subpaths for given path walking upwards to the root.

path - String pathname

Returns an Array of String paths.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/editor_config.rb', line 267

def self.traverse(path)
  paths = []
  parts = path.split("/", -1)

  idx = parts.length - 1

  while idx > 0
    paths << parts[0, idx].join("/")
    idx -= 1
  end

  if path.start_with?("/")
    paths[-1] = "/"
  else
    paths << ""
  end

  paths
end