Skip to content

Conversation

@nbfalcon
Copy link
Contributor

@nbfalcon nbfalcon commented Jan 9, 2021

  • Add an include guard
  • Remove an include guard
  • Add #pragma once
  • Remove #pragma once
  • Toggle between include guards/#pragma once
  • All of the above exposed in the menu, context sensitive (i.e. no "remove
    include guards" if there are none)
  • Skips comments at the start and end of the buffer (i.e. copyright
    boilerplates, "LocalWords: ")
  • A lot of customization options (custom include guard symbol generators, text
    after #endif, spaces after #, ...)
  • Handles lots of tricky edge cases:
#ifndef Q
#define Q

in\
t main() {}

#endif /* Q */```

->

```c++
#ifndef Q
#define Q

in\
t main() {}

#endif /* Q */

Fixes #62.


Refactor the code to jump to the start of a header skipping comments and
whitespace into a separate function, since that is used by 4 functions
now.
Refactor `emr-cc-delete-*' to return t if they deleted something. Based on this,
`emr-cc-toggle-include-guard' can see if there was an include guard/#pragma once
and insert the other.
`emr-cc-include-guard-suffix' can now also be a format string.
`emr-cc-pragma-once-space', which behaves exactly like
`emr-cc-include-guard-space' now controls whether there should be a space after
the # of `emr-cc-add-pragma-once'.

Refactor `emr-cc--include-guard-space' to take a variable instead of using
`emr-cc-include-guard-space' directly.
Use `beginning-of-defun' so that a \ continued statement will be handled
correctly.

Add a docstring.
Refactor the code to `looking-at' the include guards and #pragma once directives
into a separate function, since it is needed to delete those directives and to
check if they are present (for the context-sensitive refactor menu).
Now a space before the # in include-guard or pragma once directives is properly
handled, so `emr-cc-delete-*' will work in such cases.

Supporting generating that in `emr-cc-add-*' isn't worth it though, because
include guards are at the top-level, so need not be indented.
The attempt to make comment skipping more robust caused a regression, because
`beginning-of-defun' also goes to the start of a comment. Use the old
`forward-sexp' + `backward-sexp' heuristic instead, even though it fails if a
header starts with a delimited statement (e.g. in\\nt). This is pretty obscure
though.

Finding #endif should not suffer from that problem.

Move the code to go the end of the buffer skipping comments into a separate
function, even though it is used only once, so that such functions can be close
together (`emr-cc--beggining-of-header' and `emr-cc--end-of-header').
Handle the in\\x by temporarily modifying the syntax table to have ?\\ as space.
Use `emr-cc--end-of-header' + `forward-line' for #endif. This way, trailing
header comments are not encased in the include guard, which would allow
something like "LocalWords: " to not be wrapped.
@nbfalcon nbfalcon changed the title emr-cc-add-include-guard: skip trailing comments Include guard refactors Jan 9, 2021
@nbfalcon nbfalcon marked this pull request as draft January 17, 2021 11:32
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

Successfully merging this pull request may close these issues.

Add refactorings for include guards and #pragma once

1 participant