Clang Format - How to remove space between [ and ::namespace? - clang-format

Clang-format inserts pretty annoying whitespace in the example below:
blahblah[ ::namespace]
I want it to be formatted like this:
blahblah[::namespace]
Is there a way to do this?
Edit:
I've tried setting SpacesInSquareBrackets: false, but it doesn't help.

Starting with clang-format 3.9.0, that space is no longer inserted. In fact, now you can't get that space back even with SpacesInSquareBrackets: true.

Related

Force clang-format to respect doxygen block comments

I use long block C-style Doxygen comments in my C/C++ code. This is style #4 listed on http://www.doxygen.nl/manual/docblocks.html and looks like this (running out to 80 characters)...
/**************************************************************************//**
* \file
* \date 2017-01-02
* \author Alex Hogen
******************************************************************************/
If I run clang-format on this, it inserts a single space between the two forward slashes, so it looks all goofy like this....
/**************************************************************************/ /**
* \file
* \date 2017-01-02
* \author Alex Hogen
******************************************************************************/
I have SpacesBeforeTrailingComments set to 2, so that can't be the problem.
Tried CommentPragmas regex \/\*+\/\/\*+.
Tried CommentPragmas regex /\*(.+\n.+)+\*/
I've tried setting ReflowComments to false
... but none of those things have worked.
I understand there are two comments in this block, but I can't find any clang-format parameter addressing block comments on the same line. How can I stop clang-format from inserting this space?
And I don't want to solve this by disabling clang-format for every Doxygen comment block. That seems ridiculous.
Any good suggestions? :-)
In your .clang-format file:
CommentPragmas: '^\\.+'
This will make it not format comment line that starts with a backslash followed by a word. This works even though there is an asterisk before the doxygen comment because clang-format automatically ignores asterisks and whitespace at the beginning of every comment line.

Can you set clang-format's line length?

clang-format is breaking up my lines at 80 columns. Is there a way to make stop breaking lines? The documentation doesn't seem to address this.
The configuration option responsible for it is called ColumnLimit. You can remove the column limit by setting it to 0.
ColumnLimit: 0
Find ColumnLimit (under "Configurable Format Style Options" heading) on that page and you'll find the following statement:
ColumnLimit (unsigned)
The column limit.
A column limit of 0 means that there is no column limit. In this case,
clang-format will respect the input’s line breaking decisions within
statements unless they contradict other rules.
Source: Clang-Format Docs (v4.0.0, latest). Italics added for emphasis.
So, just like the docs say, set...
ColumnLimit: 0
... and you should be set.
I had the same issue too - I am using the C++ extension in VSCode and setting ColumnLimit: 0 still wrapped at 80. I worked around this by setting ColumnLimit to a large number; I set ColumnLimit: 200 and it works great.

Remove \text generated by TeXForm

I need to remove all \text generated by TeXForm in Mathematica.
What I am doing now is this:
MyTeXForm[a_]:=StringReplace[ToString[TeXForm[a]], "\\text" -> ""]
But the result keeps the braces, for example:
for a=fx,
the result of TeXForm[a] is \text{fx}
the result of MyTeXForm[a] is {fx}
But what I would like is it to be just fx
You should be able to use string patterns. Based on http://reference.wolfram.com/mathematica/tutorial/StringPatterns.html, something like the following should work:
MyTeXForm[a_]:=StringReplace[ToString[TeXForm[a]], "\\text{"~~s___~~"}"->s]
I don't have Mathematica handy right now, but this should say 'Match "\text{" followed by zero or more characters that are stored in the variable s, followed by "}", then replace all of that with whatever is stored in s.'
UPDATE:
The above works in the simplest case of there being a single "\text{...}" element, but the pattern s___ is greedy, so on input a+bb+xx+y, which Mathematica's TeXForm renders as "a+\text{bb}+\text{xx}+y", it matches everything between the first "\text{" and last "}" --- so, "bb}+\text{xx" --- leading to the output
In[1]:= MyTeXForm[a+bb+xx+y]
Out[1]= a+bb}+\text{xx+y
A fix for this is to wrap the pattern with Shortest[], leading to a second definition
In[2]:= MyTeXForm2[a_] := StringReplace[
ToString[TeXForm[a]],
Shortest["\\text{" ~~ s___ ~~ "}"] -> s
]
which yields the output
In[3]:= MyTeXForm2[a+bb+xx+y]
Out[3]= a+bb+xx+y
as desired.
Unfortunately this still won't work when the text itself contains a closing brace. For example, the input f["a}b","c}d"] (for some reason...) would give
In[4]:= MyTeXForm2[f["a}b","c}d"]]
Out[4]= f(a$\$b},c$\$d})
instead of "f(a$\}$b,c$\}$d)", which would be the proper processing of the TeXForm output "f(\text{a$\}$b},\text{c$\}$d})".
This is what I did (works fine for me):
MyTeXForm[a_] := ToString[ToExpression[StringReplace[ToString[TeXForm[a]], "\\text" -> ""]][[1]]]
This is a really late reply, but I just came up against the same issue and discovered a simple solution. Put a space between the variables in the Mathematica expression that you wish to convert using TexForm.
For the original poster's example, the following code works great:
a=f x
TeXForm[a]
The output is as desired: f x
Since LaTeX will ignore that space in math mode, things will format correctly.
(As an aside, I was having the same issue with subscripted expressions that have two side-by-side variables in the subscript. Inserting a space between them solved the issue.)

Removing accents/diacritics from string while preserving other special chars (tried mb_chars.normalize and iconv)

There is a very similar question already. One of the solutions uses code like this one:
string.mb_chars.normalize(:kd).gsub(/[^x00-\x7F]/n, '').to_s
Which works wonders, until you notice it also removes spaces, dots, dashes, and who knows what else.
I'm not really sure how the first code works, but could it be made to strip only accents? Or at the very least be given a list of chars to preserve? My knowledge of regexps is small, but I tried (to no avail):
/[^\-x00-\x7F]/n # So it would leave the dash alone
I'm about to do something like this:
string.mb_chars.normalize(:kd).gsub('-', '__DASH__').gsub
(/[^x00-\x7F]/n, '').gsub('__DASH__', '-').to_s
Atrocious? Yes...
I've also tried:
iconv = Iconv.new('UTF-8', 'US-ASCII//TRANSLIT') # Also tried ISO-8859-1
iconv.iconv 'Café' # Throws an error: Iconv::IllegalSequence: "é"
Help please?
it also removes spaces, dots, dashes, and who knows what else.
It shouldn't.
string.mb_chars.normalize(:kd).gsub(/[^x00-\x7F]/n, '').to_s
You've mistyped, there should be a backslash before the x00, to refer to the NUL character.
/[^\-x00-\x7F]/n # So it would leave the dash alone
You've put the ‘-’ between the ‘\’ and the ‘x’, which will break the reference to the null character, and thus break the range.
I'd use the transliterate method. See http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate
It's not as neat as Iconv, but does what I think you want:
http://snippets.dzone.com/posts/show/2384

Customising word separators in vi

vi treats dash - and space as word separators for commands such as dw and cw.
Is there a way to add underscore _ as well?
I quite often want to change part of a variable name containing underscores, such as changing src_branch to dest_branch. I end up counting characters and using s (like 3sdest), but it would be much easier to use cw (like cwdest).
Is there a way to add underscore _ as well?
:set iskeyword-=_
What is, and is not a member character to keywords depends on the language. For help on iskeyword use :help iskeyword.
In case you're using vim, you can change that by setting the iskeyword option (:he iskeyword). If that is not an option, you can always use ct_ instead of counting.
One other good option in such cases is to use camelcasemotion plugin.
It adds new motions ,b, ,e, and ,w, which work analogously with b, e, and w, except that they recognize CamelCase and snake_case words. With it you can use
c,edest
and this will replace "src_branch" with "dest_branch" if your cursor was on first character of "src_branch".
You could type cf_dest_ and save the counting part.
Edit: or as suggested: ct_ changes text until right before the underline character. (I'm using the f motion more, so it came more naturally to me)
Or you could redefine 'iskeyword' (:help iskeyword for details).
I was just looking at this myself and added this to my .vimrc:
set iskeyword=!-~,^*,^45,^124,^34,192-255,^_
My .vimrc had issues with ^| and ^", which was part of the default iskeyword for my setup, so I changed to their ascii values and it works fine. My main modification was to add "^_" to the end of the default setting to keep vim from seeing underscore as being part of a word.
To delete to the next underscore enter "df_"
To change to the next underscore enter "cf_"
NOTE: don't include the double quotes.

Resources