Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Successfully configured Howl to detect indentation from opened file - next steps? #600

Open
cloudrac3r opened this issue Jan 11, 2022 · 0 comments

Comments

@cloudrac3r
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant