Is there a simple way to refactor file indentation for a while code base? - indentation

I have a project which started with 4-space indentation, then switched to 2-space for some files only.
Now I have .editoconfig file to standardize the indentation
*.{ts}]
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
Is there a way to apply this new rules to EXISTING code?

I found out Resharper by Jetbrains does exactly the job. Unfortunately, available only for Windows

We used ESLint for this purpose. We created a special .eslintrc.json in project root with this rules and used the --fix commandline option.
But you have to remove all other eslint config files from the project beforehand or you will get the rules defined elsewhere also be autofixed.

Related

How to avoid Add this or me qualification issue in visual studio 2017 15.9.13

How can we resolve the below error. Same code works without any errors in previous visual studio versions. Only after upgrading I am getting this error. It is not possible to add 'this' to all the properties across the big solution. Hence is there any easy away we are avoid this error.
Made changes to editor config file and it started working.
# Remove the line below if you want to inherit .editorconfig settings from higher
directories
root = true
# C# files
[*.cs]
# this. and Me. preferences
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_property = false:silent
dotnet_style_qualification_for_method = false:silent
dotnet_style_qualification_for_event = false:silent
I'm using Resharper and found that Visual Studio (2019) was adding the this qualifier on code completion which would then trigger resharper's Remove redundant 'this' qualifier suggestion.
For some reason updating these settings via the gui (Reshaper nor Visual Studio settings) did not have any effect. Updating the .editorconfig however worked immediately for me. Thanks #Deepak!.
To disable suggestions and code completion from adding this qualifier.
# "This." and "Me." qualifiers
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
To enforce set to true and select the desired severity. Either true:warning or true:error.
See Language conventions

WebStorm: configure Prettier to use tabs?

The Prettier docs say to use --use-tabs to override the default behavior. WebStorm automatically populated the Prettier package:
That's a folder. I tried changing it to npm\prettier.cmd and adding the switch, but that doesn't work (It just turns red).
How can I set WebStorm to use tabs with Prettier?
If your project has a .editorconfig file, you can override the indent_style setting in there.
# top-most EditorConfig file
root = true
[*]
indent_size = 4
indent_style = tab
I found these resources helpful too:
https://prettier.io/docs/en/options.html#tabs
https://editorconfig.org/
Due to https://github.com/prettier/prettier/pull/2434 fix, Prettier (even global!) uses configuration file nearest to current file (up the directory tree) when formatting.
So, you can install it globally and configure it in your project, by adding the corresponding .prettierrc file or "prettier" section in project package.json.

Can you glob source code with meson?

Is it possible to glob source code files in a meson build?
Globbing source files is discouraged and is bad practice, and not only on Meson. It causes weird errors, makes it hard to have some developement files aside for development but that you don't want to build or ship, and can cause problems with incremental builds.
Explicit is better than implicit.
2021-03-02 EDIT:
Read also Why can't I specify target files with a wildcard? in the Meson FAQ.
Meson does not support this syntax and the reason for this is simple. This can not be made both reliable and fast.
If after all the warnings, you still want to do it at your own risk, the FAQ tells you how in But I really want to use wildcards!. You just use an external script to do the globbing and return the list of files (that script is called grabber.sh in that example).
c = run_command('grabber.sh')
sources = c.stdout().strip().split('\n')
e = executable('prog', sources)
I found an example in the meson unit tests showing how to glob source, but in the comments it says this is not recommended.
if build_machine.system() == 'windows'
c = run_command('grabber.bat')
grabber = find_program('grabber2.bat')
else
c = run_command('grabber.sh')
grabber = find_program('grabber.sh')
endif
# First test running command explicitly.
if c.returncode() != 0
error('Executing script failed.')
endif
newline = '''
'''
sources = c.stdout().strip().split(newline)
e = executable('prog', sources)
The reason this is not recommended: attempting to add files by glob'ing a directory will NOT make those files automatically appear in the build. You have to manually re-invoke meson for the files to be added to the build. Re-invoking ninja or other back-ends is not sufficient, you must reinvoke meson itself.
meson.build
glob = run_command('python', 'glob')
sources = glob.stdout().strip().split('\n')
glob:
import glob
sources = glob.glob('./src/*.cpp') + glob.glob('./src/**/*.cpp')
for i in sources:
print(i)
No it's not possible. Every sources have to be explicitly stated to build a target.

Custom autoformat in Xcode [duplicate]

As a C# developer, I have become highly dependent on the automatic formatting in Visual Studio 2008. Specifically, I will use the CTRL + K , D keyboard shortcut to force things back into shape after my sloppy implementation.
I am now trying to learn Objective-C and am missing certain features in Xcode, but probably none are quite as painful as the formatting shortcut. My Google searches have yielded nothing built in, though it seems there are some hacks. Am I missing something or does this feature not exist natively in Xcode?
That's Ctrl + i.
Or for low-tech, cut and then paste. It'll reformat on paste.
Unfortunately, Xcode doesn't have anything nearly as extensive as VS or Jalopy for Eclipse available. There are SOME disparate features, such as Structure > Re-Indent as well as the auto-formatting used when you paste code into your source file. I am totally with you, though; there definitely should be something in there to help with formatting issues.
I'd like to recommend two options worth considering. Both quite new and evolving.
ClangFormat-Xcode (free) - on each cmd+s file is reformatted to specific style and saved, easy to deploy within team
An Xcode plug-in to format your code using Clang's format tools, by
#travisjeffery.
With clang-format you can use Clang to format your code to styles such
as LLVM, Google, Chromium, Mozilla, WebKit, or your own configuration.
Objective-Clean (paid, didn't try it yet) - app raising build errors if predefined style rules are violated - possibly quite hard to use within the team, so I didn't try it out.
With very minimal setup, you can get Xcode to use our App to enforce
your rules. If you are ever caught violating one of your rules, Xcode
will throw a build error and take you right to the offending line.
In xcode, you can use this shortcut to Re-indent your source code
Go to file, which has indent issues, and follow this :
Cmd + A to select all source codes
Ctrl + I to re-indent
Hope this helps.
My personal fav PrettyC wantabe is uncrustify: http://uncrustify.sourceforge.net/. It's got a few billion options however so I also suggest you download UniversalIndentGUI_macx, (also on sourceforge) a GUI someone wrote to help set the options the way you like them.
You can then add this custom user script to uncrustify the selected text:
#! /bin/sh
#
# uncrustify!
echo -n "%%%{PBXSelection}%%%"
/usr/local/bin/uncrustify -q -c /usr/local/share/uncrustify/geo_uncrustify.cfg -l oc+ <&0
echo -n "%%%{PBXSelection}%%%"
You can use Command + A to select all content and next Ctrl + I to format the selected content.
I also feel xcode should have this function.
So I made an extension to do it: Swimat
Simple install by brew cask install swimat
You can give it a try, see https://github.com/Jintin/Swimat for more information.
Cmd A + Ctrl I
Or Cmd A And then Right Click. Goto Structure -> Re-Indent
Consider buying yourself a license for AppCode, an intelligent Objective-C IDE that helps iOS/OS X developers. AppCode is fully compatible with Xcode, but goes beyond Xcode in adding powerful features.
AppCode an Objective-C variant of the Intellij IDEA IDE from JetBrains. They are also authors of popular ReSharper extension to Visual Studio, which main purpose from here seems like a desperate attempt to bring a touch of IDEA experience to a Microsoft product.
AppCode is using its own code analyser which gives close-to-perfect refactoring and code navigation support. There is an ability to re-indent and completely reformat code also (although I still keep missing a couple of formatting settings in hard cases, but mostly it works well).
You might try the trial version, of course.
Swift - https://github.com/nicklockwood/SwiftFormat
It provides Xcode Extension as well as CLI option.
CTRL + i
that's it.
(no COMMAND + i)
You can also have a look at https://github.com/octo-online/Xcode-formatter which is a formatter based on Uncrustify and integrated into Xcode. Works like a charm.
You could try that XCode plugin https://github.com/benoitsan/BBUncrustifyPlugin-Xcode
Just clone github repository, open plugin project in XCode and run it. It will be installed automatically. Restart Xode before using formatter plugin.
Don't forget to install uncrustify util before. Homebrew, for exmaple
brew install uncrustify
P.S. You can turn on "after save formatting" feature at Edit > Format Code > BBUncrustifyPlugin Preferences > Format On Save
Hope this will be useful for u ;-)
I suggest using ClangFormat. In order to install, please follow these steps:
Install Alcatraz package manager for XCode
Supports Xcode 5+ & OS X 10.9+
After installation restart XCode.
Open XCode -> Window Menu -> Package Manager
Search (find) ClangFormat and install it. After installation again restart XCode.
Now at XCode menu you can use Edit -> Clang Format submenu for formatting.
You can choose different types of formatting. Also by enabling Format On Save you can gain auto-format capability.
If your Xcode version 3.x , you should use "User Script" With Uncrustify , here this a Example:
#!/bin/sh
echo -n "%%%{PBXSelection}%%%"
$YOURPATH_TO_UNCRUSTIFY/uncrustify -q -c $YOURPATH_TO_UNCRUSTIFY_CONFIG/CodeFormatConfig.cfg -l OC+
echo -n "%%%{PBXSelection}%%%"
add above to your Xcode "User Script".
if Xcode version 4.x , I think you should read this blog : Code Formatting in Xcode 4,
In this way , used the "Apple Services" , but it's not good enough , cause too slow experience, does anyone has the same thing ?
why apple drop "user script" .... xD
First, Examine XCode Preferences "Indentation" section. You can customize things quite a bit there...
For more fine grained control, refer to the XCode User Defaults document from apple. (May require a developer login to view). For example, I was able to disable the "indent on paste" by entering the following in terminal:
defaults write com.apple.XCODE PBXIndentOnPaste No
to read back your setting:
defaults read com.apple.XCODE PBXIndentOnPaste
This only works for languages with are not whitespace delineated, but my solution is to remove all whitespace except for spaces, then add a newline after characters that usually delineate EOL (e.g. replace ';' with ';\n') then do the ubiquitous ^+i solution.
I use Python.
Example code, just replace the filenames:
python -c "import re; open(outfile,'w').write(re.sub('[\t\n\r]','',open(infile).read()).replace(';',';\n').replace('{','{\n').replace('}','}\n'))"
It 's not perfect (Example: for loops), but I like it.
We can use Xcode Formatter which uses uncrustify to easily format your source code as your team exactly wants to be!.
Installation
The recommended way is to clone GitHub project or download it from https://github.com/octo-online/Xcode-formatter and add the CodeFormatter directory in your Xcode project to get :
Xcode shortcut-based code formatting: a shortcut to format modified sources in the current workspace
automatic code formatting: add a build phase to your project to format current sources when application builds
all sources formatting: format all your code with one command line
your formatting rules shared by project: edit and use a same configuration file with your project dev team
1) How to setup the code formatter for your project
Install uncrustify
The simplest way is to use brew:
$ brew install uncrustify
To install brew:
$ ruby –e “$(curl –fsSkl raw.github.com/mxcl/homebrew/go)”
Check that uncrustify is located in /usr/local/bin
$ which uncrustify
If your uncrustify version is lower than 0.60, you might have to install it manually since modern Objective-C syntax has been added recently.
Add CodeFormatter directory beside your .xcodeproj file
Check that your Xcode application is named "Xcode" (default name)
You can see this name in the Applications/ directory (or your custom Xcode installation directory). Be carefull if you have multiple instances of Xcode on your mac: ensure that project's one is actually named "Xcode"!
(Why this ? This name is used to find currently opened Xcode files. See CodeFormatter/Uncrustify_opened_Xcode_sources.workflow appleScript).
Install the automator service Uncrustify_opened_Xcode_sources.workflow
Copy this file to your ~/Library/Services/ folder (create this folder if needed).Be careful : by double-clicking the .workflow file, you will install it but the file will be removed! Be sure to leave a copy of it for other users.
How to format opened files when building the project
Add a build phase "run script" containing the following line:
sh CodeFormatter/scripts/formatOpendSources.sh
How to format files in command line
To format currently opened files, use formatOpenedSources.sh:
$sh CodeFormatter/scripts/formatOpendSources.sh
To format all files, use formatAllSources.sh:
$sh CodeFormatter/scripts/formatAllSources.sh PATH
PATH must be replaced by your sources path.
E:g; if project name is TestApp then the command will be
$sh CodeFormatter/scripts/formatAllSources.sh TestApp
it will look for all files in the project and will format all the files as configured in uncrustify_objective_c.cfg file.
How to change formatter’s rules
Edit CodeFormatter/uncrustify_objective_c.cfg open with TextEdit
Well I was searching for an easy way. And find out on medium.
First to copy the json text and validate it on jsonlint or something similar. Then to copy from jsonlint, already the json is formatted. And paste the code on Xcode with preserving the format, shortcut shift + option + command + v

waf: nested projects and _cache.py: not supported?

I am converting a project from autotools to waf with the hope that it can be easily compiled in windows as well.
I am using a super project with two children folders that are 2 projects.
One of them is a library, the other, a program, like this:
superproject/wscript
superproject/libraryproject/wscript
superproject/programproject/wscript
It seems that waf has terrible support for subprojects. I have a wscript in each of these directories.
I recurse from superproject into the 2 other projects, but the _cache.py file is shared for both projects. This has the following side effects (issues):
When using the boost tool, I had to use it like this to avoid name collisions:
# In library project
cfg.check_boost('boost_program_options', uselib_store='BOOST_LIBRARYPROJECT')
# In program project
cfg.check_boost('boost_program_options', uselib_store='BOOST_PROGRAMPROJECT')
boost-libs and boost-includes command line options are also lost by default, so I have to set them manually, like this:
cfg.env.LIBPATH_BOOST_PROGRAMPROJECT = cfg.options.boost_libs
...
The _cache.py file is overwritten by the programproject/wscript, loosing all the configuration for the flags.
Questions:
Is there any good way to nest projects and avoid at least issue 2?
Is there any reasonable way to avoid both that doesn't require a script and building projects separately?
Configuration file is not written twice.
My mistake was to do this:
cfg.env = ConfigSet()
I wanted a new and clean ConfigSet but doing that in both projects made the first set of flags to be lost.
Since the environment seems to be shared among all project configurations, is it good style to name the variables with custom names? For example, instead of using:
cfg.check_boost('program_options')
Should I use:
cfg.check_boost('program_options', uselib_store='BOOST_MYPROGRAMPROJECT')
Is this good style or it's usually done in another way?
Can be done in a cleaner way deriving ConfigSets?