You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Files scattered around my computer have many different kinds of indentation. I want to detect what kind of indentation the file already uses and then use that for future editing.
After poking around the documentation and MoonScript for the first time I managed to get this working. I don't know whether it's good form or anything but it seems to work. Maybe this will be helpful for future readers.
set_indentation_for_buffer = (parameters) ->
b = parameters.buffer
-- find the first line that starts with whitespace
i = 1
while b.lines[i] != nil
starting_spaces = string.match b.lines[i].text, '%s*'
i = i + 1
if 0 == string.len starting_spaces
continue
-- found a line that starts with whitespace, the whitespace part is in the variable starting_spaces
indent_char = starting_spaces[1]
-- if indentation is tabs
if indent_char == '\t'
-- set to tabs mode without editing default widths
b.config.use_tabs = true
log.warn 'indenting using tabs with default width'
-- if indentation is spaces
elseif indent_char == ' '
-- load width from line
width = string.len starting_spaces
b.config.use_tabs = false
b.config.indent = width
b.config.tab_width = width
log.warn "indenting using spaces with width #{width}" -- old debug values: due to line '#{starting_spaces}' of '#{b.lines[i-1].text}'"
return
howl.signal.connect 'file-opened', set_indentation_for_buffer
howl.signal.connect 'buffer-reloaded', set_indentation_for_buffer
This does rely on tab-width and indent global config values both being the same, which they aren't by default.
I think this should be a standard feature included with all code editors. Would it be possible to make this pretty enough to include it in the main codebase?
The text was updated successfully, but these errors were encountered:
Files scattered around my computer have many different kinds of indentation. I want to detect what kind of indentation the file already uses and then use that for future editing.
After poking around the documentation and MoonScript for the first time I managed to get this working. I don't know whether it's good form or anything but it seems to work. Maybe this will be helpful for future readers.
This does rely on
tab-width
andindent
global config values both being the same, which they aren't by default.I think this should be a standard feature included with all code editors. Would it be possible to make this pretty enough to include it in the main codebase?
The text was updated successfully, but these errors were encountered: