rosshemsley / sublimeclangformat Goto Github PK
View Code? Open in Web Editor NEWA C++ code formatter based on Clang Format, for beautiful code with minimal effort in Sublime Text 3
License: Other
A C++ code formatter based on Clang Format, for beautiful code with minimal effort in Sublime Text 3
License: Other
There is a important new feature I would like to have that is only available in clang format 3.7, which is "AlignConsecutiveAssignments". I am able to run clang format 3.7 with this feature on command line. After sometime of digging into why passing this argument has no effect on sublime, I came to the conclusion that It looks like the fix is to add the new settings in all_settings in clang_format.py. Please update. Thank you!
Great tool!
I would love the feature to format the area just after pasting some code. Any idea of how complex this would be to implement, or pointers to where I should start to achieve this.
Cheers.
I have been searching for this issue for several days, and have not found any solution to this.
As the title says, the screen of ST jumps to the middle or rightmost when formatting.
Here is my custom clang-format config.
{
"BasedOnStyle" : "Google",
"IndentWidth" : 5,
"AllowShortIfStatementsOnASingleLine" : "Always",
"TabWidth" : 4,
"AlignEscapedNewlinesLeft" : true,
"AllowShortIfStatementsOnASingleLine": true,
}
Please help!
Thank you.
How to reproduce 1. open file /* Header with NLS UTF-8 symbols ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда ываофыдаоывоалдывоадлыовадоывдаоыдлодоадфываоыдаоыфдаоыдваоыалыда */ int main(void) { print_descriptor(0, 0, 0); print_descriptor(0, 0x000FFFFF, (GDT_CODE_PL0)); print_descriptor(0, 0x000FFFFF, (GDT_DATA_PL0)); print_descriptor(0, 0x000FFFFF, (GDT_CODE_PL3)); print_descriptor(0, 0x000FFFFF, (GDT_DATA_PL3)); return 0; } int main(void) { // start selection here print_descriptor(0, 0, 0); print_descriptor(0, 0x000FFFFF, (GDT_CODE_PL0)); print_descriptor(0, 0x000FFFFF, (GDT_DATA_PL0)); print_descriptor(0, 0x000FFFFF, (GDT_CODE_PL3)); print_descriptor(0, 0x000FFFFF, (GDT_DATA_PL3)); return 0; } int main(void) { print_descriptor(0, 0, 0); print_descriptor(0, 0x000FFFFF, (GDT_CODE_PL0)); print_descriptor(0, 0x000FFFFF, (GDT_DATA_PL0)); print_descriptor(0, 0x000FFFFF, (GDT_CODE_PL3)); print_descriptor(0, 0x000FFFFF, (GDT_DATA_PL3)); return 0; } int main(void) { print_descriptor(0, 0, 0); print_descriptor(0, 0x000FFFFF, (GDT_CODE_PL0)); print_descriptor(0, 0x000FFFFF, (GDT_DATA_PL0)); print_descriptor(0, 0x000FFFFF, (GDT_CODE_PL3)); print_descriptor(0, 0x000FFFFF, (GDT_DATA_PL3)); // end selection here and press ctrl+alt+a return 0; } 2. select in bottom code 3. press ctrl+alt+a 4. nothing happened (in correct variant we should see remove intent from 0x000FFFFF PROBLEM SOURCE Offset option of clang get bytes offset (not a symbols offset) -offset= - Format a range starting at this byte offset. Multiple ranges can be formatted by specifying several -offset and -length pairs. Can only be used with one input file. PATCH sorry can't remove PEP8 decoration --- /tmp/clang-format.orig/clang_format.py +++ /Packages/ClangFormat/clang_format.py @@ -230,42 +248,54 @@ else: regions = self.view.sel() + # Get all view to buf + buf = self.view.substr(sublime.Region(0, self.view.size())) + buf_bytes = buf.encode(encoding) + # Deal with all selected regions. for region in regions: region_offset = region.begin() - print("region_offset="+region_offset) region_length = region.size() view = sublime.active_window().active_view() + + # Get selection + sel = self.view.substr(region) + sel_bytes = sel.encode(encoding) # If the command is run at the end of the line, # Run the command on the whole line. if view.classify(region_offset) & sublime.CLASS_LINE_END > 0: - region = view.line(region_offset) - region_offset = region.begin() - region_lenth = region.size() + region = view.line(region_offset) + + region_offset = region.begin() + region_length = len(sel_bytes) + + # Convert offset from symbols to bytes + buf_before_sel = buf[:region_offset] + buf_before_sel_bytes = buf_before_sel.encode(encoding) + region_offset = len(buf_before_sel_bytes) # Add this region to the set of offsets. command.extend(['-offset', str(region_offset), '-length', str(region_length)]) # We only set the offset once, otherwise CF complains. - command.extend(['-assume-filename', str(self.view.file_name())] ) + command.extend(['-assume-filename', str(self.view.file_name())]) # TODO: Work out what this does. # command.extend(['-output-replacements-xml']) # Run CF, and set buf to its output. - buf = self.view.substr(sublime.Region(0, self.view.size())) + startupinfo = None if os_is_windows: startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW - p = subprocess.Popen(command, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, stdin=subprocess.PIPE, - startupinfo=startupinfo) - output, error = p.communicate(buf.encode(encoding)) - + p = subprocess.Popen(command, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, stdin=subprocess.PIPE, + startupinfo=startupinfo) + output, error = p.communicate(buf_bytes) # Display any errors returned by clang-format using a message box, # instead of just printing them to the console. Also, we halt on all # errors: e.g. We don't just settle for using using a default style.
In some files I turn off clang-format for the entire file using // clang-format off
comments. In these cases, I may still want to format a selection of the file, which is tricky, because clang format selection
has no effect due to // clang-format off
. So I need to copy the selection, paste it in a different file, clang-format it there, and paste the result back in the original. file.
It would be nice if that could be automated with a new option: "Force clang-format selection"
Works fine on ST2 with a few formatting changes (basically just removing trailing commas) to config files. So, why is it not supported officially?
Initially I was suprised to see in package control that sublimeclang works on st2, whereas clang-format doesnt.
The readme currently shows this:
{
"folders": [],
"settings": {
"ClangFormat": {
"format_on_save": true
}
}
}
But really this doesn't work. It needs to be this:
{
"format_on_save": true
}
I format only using spaces, but work with files that might contain both tabs and spaces. When working in some files that have both and try to format only a few lines, all the tabs in the file are converted to spaces. If the file is large, this creates a big diff and can be quite annoying to revert all the spaces to tabs. Could we have only the selected lines be changed instead of the whole file ?
Thanks :)
I need to pass IncludeBlocks: Preserve
into clang-format
and I don't know how to do it. The config file in the root of my project directory is just ignored. clang-format
works properly when being launched from terminal.
I'm on Ubuntu 12 with clang-format 3.4. The other predefined format styles work fine. However, when I try to edit and use the settings file for a custom format options, it doesn't recognize any of the key.
I get an error: unknown key,
then it says
Error parsing-style: invalid argument
It doesn't seem to matter which key I use, they all fail in the same manner.
Please add a fallback-style setting to the user settings, that will add the fallback-style parameter from clang-format. I don't want any formatting when not style file is provided. Thanks in advance!
I do not want SortIncludes my #include files, so I add setting
{
...
"SortIncludes": false
...
}
is not worked.
My Config is sublime-text-3, clang-format 3.8
clang_format_custom.sublime-settings is completely ignored
{
"BasedOnStyle": "Google",
"AlignConsecutiveAssignments": true,
"ColumnLimit": 100,
"UseTab": "Never",
"TabWidth": 4,
}
The current clang-format is 3.9; is it possible to update the plugin to handle the newer options (http://clang.llvm.org/docs/ClangFormatStyleOptions.html)?
Thanks, Ákos
Hi,
I found a problem in the llvm plugin that I think might also happen in your version. I don't have ST3 so I can't check. It would be cool if you could join the discussion on the cfe-dev mailinglist.
See here:
http://lists.llvm.org/pipermail/cfe-dev/2015-August/044561.html
Hello,
thanks for this very useful tool! I have a question related to bracewrapping
, because I cannot make it work. A standard clang-format
file should have something like this:
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
How to replicate this in a custom style for this package?
I don't want to format .js
files with SublimeClangFormat, only .cpp
files. Is there any way to exclude files types from being formatted?
The links in the README will go dead. They clang-format
binaries are extremely out of date anyway and should not be used.
I hope this plugin can recognize the view's setting of tab_size
automatically and use it in the process of formatting.
Great Package! The clang-format has several styles to have, but I would like to use my own prefered style, I noticed there is “Custom” in the “Select Style” option, but I didn’t know how to configure that, I know the configuration file is exported as .clang-format, but how to apply it in the package?
Hi,
I have some .cpp files in ./src and their .h files in ./include. In the .cpp files, a bunch of errors show up saying, "could not find this file" when including the .h files, or "unknown type" when declaring objects from classes defined in the includes. Is there a way for the plugin to detect these other libraries? I am just beginning to program in cpp, I am sorry if the question is trivial or I said something wrong.
Thanks a lot for the help!
Raimon
For some reason, even though I have format_on_save as true, the file doesn't format. Clicking super+option+a seems to work though.
This is my config:
{
"style": "Google",
"format_on_save": true
}
set the path and then do the command to set format style but doesnot work
Often I am working with another person's codebase, and need to disable fixing so I don't touch every line in a commit, of a file with a quick-change. I find this very handy, from standard format:
This is under "Sublime Text" / "Settings"
Also handy to add it to the command-palette:
Looks like they do it in the menu file. I can add a PR, if you're interested.
There is an important new feature I would like to have that is only available in clang format 3.8, which is "AlignOperands". I am able to run clang format 3.8 with this feature on command line. Please update. Thank you!
I opened a pull request a week ago but got no response. Please look into it.
We have our custom file in a certain directory. Sadly it seems like I cannot set a custom path to that format file.
I started to make a Clang Format Sublime plugin before I discovered your plugin. Great work! One thing I'd like to ask is: would it be possible to add support for Objective-C, Objective-C++. The capability is already there in the clang-format binary, so it shouldn't be hard to add it.
In Sublime, I've enabled format on save, but the clang-format only applies on the current line the cursor is on, is it possible to clang format the whole file?
Hello and thanks for this plug-in, it was a breeze to install and get up and running.
I was wondering, is it possible to use a standard config file for this plug-in, written in yaml (such as ~/.clang-format
or one at a user specified path)? I'd like this so that I could re-use the clang-format settings that are used by a different application.
cheers,
Rich
Is it possible to include a setting to ignore formatting certain folders / files? I have a bunch of thirdparty packages as submodules, and auto-formatting them on save messes up my builds!
Hi There!
I like this plugin but when clang-format is executed, the code scrolls horizontally (to the right) a bit. So if I use the format on save option, then each time I save it will scroll a bit. I don't use word-wrap and sometimes my code will continue further than the size of the editor.
I love using this plugin but this kind of irritates me!
Is there something we can do about this?
Many thanks on any help on this matter!
Regards!
In linux and window, the option key is referred to as the alt key. This means that the keybindings do not work on Linux and Windows.
Solution: Change the option in Keybinding (Windows & Linux) to say alt instead of option.
Hi
What should I do to automatically format a file when saving?
Additionally, if possible, I would like to enable that setting to my current project/workspace only (and not system wide).
Are you planning to support such feature?
Thanks
I selected a particular region and hit cmd+option+a. clang-format did its thing but it did so outside of the region I specified. This is in Sublime Text 3 build 3083.
Hi! Something about the update to solve #49 has broken my ability to format. My clang_format_custom.sublime-settings (Clang format default) reflects the update, but when I go to actually format I get the error:
Clang format: YAML:1:254: error: unknown key 'BraceWrapping'
{AlignTrailingComments: true, AllowShortLoopsOnASingleLine: true, BasedOnStyle: LLVM, ColumnLimit: 150, AllowShortIfStatementsOnASingleLine: true, AllowAllParametersOfDeclarationOnNextLine: false, BinPackArguments: false, UseTab: Never, BraceWrapping: {}, TabWidth: 4, IndentWidth: 4}
^
Error parsing -style: Invalid argument
I want to format my CUDA code (suffixes ".cu" and ".cuh") using the C++ format. How can I specify the formatter to apply to ".cu" and ".cuh" files? Is there a line I can enter in the clang_format_custom.sublime-settings
file?
I have the same style configuration written on a .clang-format
file and the clang_format_custom.sublime_settings
file (modulo whatever syntactic changes are necessary). The particular line of interest is the following.
"AlignConsecutiveMacros": "Consecutive", // .clang-format has: AlignConsecutiveMacros: Consecutive
Now, when I run this by first changing Clang Format: Select Style
to File
, then choosing Clang Format: Format Selection
, I get the correct result (most of the times): namely, the preprocessor macros of consecutive lines are aligned. However, if I change Clang Format: Select Style
to Custom
and then choose Clang Format: Format Selection
, then the macros are all unaligned.
I am seeing this bug on two of my Fedora 32 machines using Sublime Text 4 (build 4113).
Hello
do you think it's easy to "port" clang-format to CudaText?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.