Execute gofmt on file save in IntelliJ - go

I use IntelliJ with the Golang plugin. Is it possible to configure the IDE to execute gofmt on file save?

Of course you can.
install File Watchers plugin
open "Preferences->Tools->File Watchers" & Add a new watcher
set File Type: Go, Program: the abosolute path of gofmt, Arguments: -w $FilePath$
click OK

Gogland EAP 16 deprecated the On Save actions and replaced them with the File Watchers plugin.
If you have previously configured On Save actions, the IDE will prompt you to install the plugin and configure everything automatically. If you want to set it up from scratch yourself, check if you have File Watchers plugin and if not install it.
Enable gofmt:
Go to Settings | Tools | File Watchers.
Click the + button and select go fmt.
The default values are good.
In Advance Options select:
Auto-save edited files to trigger the watcher
Trigger the watcher on external changes
Click ok!
You can do the same for goimports and gometalinter.

If you are using Gogland, you have an option On Save under Preferences

Updated:
For new versions of the Goland:
open File->Settings->Tools->File Watchers
click on + button and choose go fmt
press Ok
Done :)

September 2021 Update.
If you are using GoLand with the 2021.2 version and higher (July 2021), there are a few options to enable gofmt on save besides File Watchers.
Enable Run gofmt on code reformat under Preferences/Settings | Editor | Code Style | Go | Other.
Enable Reformat code under Preferences/Settings | Tools | Actions on Save.
Press Command + S on macOS or Ctrl + S on Windows/Linux to save your changes.

Related

Debugging go in vscode doesn't stop at breakpoints, says "Could not find file ..." when debugger starts

Ubuntu. vscode 1.62.1. go1.17.3. vscode go extension v0.29.0. delve v1.7.1.
I'm new to vscode and Go. I have many years of experience debugging Java apps in Eclipse.
I've constructed a small multi-module Go app. I can set a breakpoint in main and other functions in other modules. Inside main.go, I select "Start Debugging".
It starts the application, and I can tell it's working from the console, and that the REST endpoint responds with my dummy response.
However, it will NOT stop at breakpoints. As soon as I start the session, the red breakpoint markers suddenly become hollow, and hovering on one of them shows a message "Could not find file ...", which prints the full path to the source file in question.
When I start it, it shows the following in the console:
Starting: /home/.../go/bin/dlv-dap dap --check-go-version=false --listen=127.0.0.1:43347 --log-dest=3 from /home/.../...
DAP server listening at: 127.0.0.1:43347
I haven't modified the launch.json (I hope someday a friendlier interface to editing launch configurations is provided).
What else could I be doing wrong?
Update:
This is a screenshot showing main.go just before I press F5 (Start Debugging):
Notice that I have a breakpoint on the print statement, on the first line of main.
This is what I see after I press F5:
Notice that it printed "At start of main" in the console. It didn't stop at the breakpoint. Also notice message in tooltip when hovering over the breakpoint.
Update:
This is a view of my directory structure:
First, just make sure you have initiated your project with go mod init voltagems: that would explain the import "voltagems/xxx", but also helps delve to find your main.go file at debug time.
You should have go.mod and go.sum files beside main.go.
Second, check your go env output, making sure GOPATH and GOROOT are set to default paths.
The OP David M. Karr adds in the comments:
I did run "go mod init" when I first created the project, but I realized that I didn't like the root module name, so I changed it to "voltagems"
I believe you can edit directly go.mod first line, and make sure it says:
module voltagems
Then go mod verify + go mod tidy
Finally, go build .. Restart your VSCode (or the command Reload Window), and see if the issue persists.
The OP David M. Karr points out to a root cause:
There are symbolic links in my project path.
There is a "substitutePath" configuration in VSCode-Go that is used to map to absolute paths.
You can see this parameter mentioned in Debugging with Legacy Debug Adapter
substitutePath
Path mappings to apply to get from a path in the editor to a path in the compiled program (default: []).
That comes from issue 622 "debug: breakpoints don't work when working with symlink".
And commit 93f32bb
src/debugAdapter: add substitutePath config for debugging
This change adds a new configuration option to both launch and
attach requests.
substituePath takes an array that maps from string to string that is used to translate paths passed to the debugger and then
back to the client.
This allows users to translate their symlinked directories to the
files that were actually used to build the binary.
In addition this can also be used for remote debugging, and when the location of the files has moved since the program was built.
Example: you need a from and to key:
"substitutePath": [
{
"from": "/symlink/path/dir/on/local/machine",
"to": "/absolute/path/dir/on/local/machine",
},

PhpStorm: Change SCSS File Watcher predefined settings

In PhpStorm 2016.3.2, if you open a .scss file, a file watcher will be added with default settings:
How can I modify these settings global?
If I start a new project, the file watcher will be added with the new modified settings?
All file watchers are configured on project level, there is no way to modify watchers globally. However you can use export/import functionality to export your watcher settings (see 'Import' and 'Export' buttons in Settings | Tools | File Watchers)
Related feature requests: WEB-8414, WEB-12478.

Windows 10 PhpStorm 2016.2.1 can't install new theme [duplicate]

I'm student and I got the free latest version of PhpStorm 9 (build PS-141.1912). I tried to add a theme and color scheme to PhpStorm so I put my theme (.icls file) in the C:\Users\me\.WebIde90\colors folder:
(source: noelshack.com)
Then I restarted PhpStorm, but I can't see my theme:
(source: noelshack.com)
In PhpStorm 2016.1 it will be ~\.PhpStorm2016.1\config\colors (where ~ is your user home folder - typically C:\Users\USERNAME).
Based on your screenshot and your info, you have placed them in ~\.WebIde90\colors while it should be ~\.WebIde90\config\colors (where ~ is your user home folder -- typically C:\Users\USERNAME).
In general:
A file with Color Schema (*.icls) should be put into appropriate folder and then IDE should be restarted if it was running by that time (as such stuff gets checked only on launch).
Windows: C:\Users\USERNAME\.IDE_FOLDER\config\colors
Linux: ~/.IDE_FOLDER/config/colors
Mac: ~/Library/Preferences/IDE_FOLDER/colors
Where IDE_FOLDER is the <ProductName><Version> -- e.g. PhpStorm2016.2 for the latest stable version of PhpStorm (2016.2.2).
More info on folders: https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs
P.S.
If Color Schema comes as *.jar file then use File | Import Setting...
Please also note that these editor themes are for syntax highlighting mainly and can be changed at Settings/Preferences | Editor | Colors & Fonts.
This has nothing to do with GUI Theme (also known as LAF -- Look and Feel -- styling the actual GUI elements) which is a separate thing and available at Settings/Preferences | Appearance & Behavior | Appearance.
P.S.
Instead of going into Settings/Preferences .. you can use View | Quick Switch Scheme... to quickly change between various schemas (colors/keymaps/code styles/etc.)

Fix Quick Look for TypeScript files

My Mac recognises the .ts extension as MPEG-2 Transport Stream, while I use it more often as TypeScript files. Because of that Quick Look does not show the contents of .ts files.
I found on Stackoverflow that I should be able to assign custom extensions to the text editor, but I'm not sure how.
QLColorCode is a very nice Quick Look plugin which allows you to set syntax highlighting for ANY extension file you want.
1. Install QLColorCode Quick Look plugin
If you have Homebrew, you can install it with:
brew cask install qlcolorcode
Otherwise, you can just copy / paste the QLColorCode.qlgenerator to your ~/Library/QuickLook/ folder from a release.
2. Add .ts and .tsx syntax highlighting support
First get the kMDItemContentType of the extension you want to quick look at with the plugin:
mdls -name kMDItemContentType /path/to/any/file.ts
Add an entry with the string returned by the previous command to the plugin settings list — for a .ts file, this should be public.mpeg-2-transport-stream. To do so, just open the file ~/Library/QuickLook/QLColorCode.qlgenerator/Contents/Info.plist with any text editor (it's not a compiled file) and in the section CFBundleDocumentTypes > Item 0 > LSItemContentTypes (~ 15th line), add a line like this:
<string>public.mpeg-2-transport-stream</string>
You can find more information on how to add language type support to the plugin in the project README.md.
Here is the original post where I found all this useful information.
According this article, there is another solution:
1) Remove filetype definition from: /System/Library/CoreServices/CoreTypes.bundle/Contents/Info.plist
You need to do that in recovery mode.
215 => {
"UTTypeConformsTo" => "public.movie"
"UTTypeDescription" => "MPEG-2 Transport Stream"
"UTTypeIdentifier" => "public.mpeg-2-transport-stream"
"UTTypeTagSpecification" => {
"public.filename-extension" => [
0 => "ts"
]
}
}
2) Then run
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill
I managed to get QuickLook to display .ts files as plain text.
The necessary step was to remove Movie.qlgenerator from the directory /System/Library/QuickLook.
The downside is, that QuickLook can't preview any types of movies any more...
Update 2016-04-12:
In OSX El Capitan it seems not possible to simply remove a file from /System/Library/QuickLook (probably because of System Integrity Protection).
I managed to remove the Movie.qlgenerator with the following steps:
Back up the file first
Boot into Recovery Mode: Restart and press-and-hold Cmd-R
Open a terminal
disable SIP: csrutil disable
cd /Volumes/Macintosh HD/System/Library/QuickLook (your volume name may be different)
rm -rf Movie.qlgenerator
csrutil enable
csrutil status -> Verify SIP is turned on again
Restart
After the above steps I could quick-look my .ts files.
The best part: I put the Movie.qlgenerator I had backed up in the first step into ~/Library/QuickLook and now I have also quick-look previews for movies ...

How to Minify CSS with SCSS File Watcher in PHPStorm IDE

Is there a way to configure SASS FileWatcher so it builds a Minified CSS?
I currently configured SASS + YUI Compressor to accomplish this but I would like to do this with pure SASS if possible.
Here are the screenshots of both configurations:
SASS
YUI Compressor CSS
Thanks in advance.
Probably the fastest way to achieve this is to use the compressed option, mentioned in the previous comments, as an argument. The quickest way to configure this in PHPStorm is as follows:
Go to File > Settings
Inside Project Settings select File Watchers
You should already have an SCSS watcher created here (if you have the SCSS watch plugin enabled, PHPStorm prompts you to create a watcher when opening a new .scss file.) Otherwise, enable it (more info about that in this section of the official documentation,) and then create the new watcher pressing the "+" symbol.
Double click the watcher name to access its configuration.
In the Arguments line make sure to add the --style compressed argument
Click OK and you're done
This image shows how that configuration should look:
From that point on, your .css output files will be compressed.
The correct answer is --style=compressed
menu File -> Settings -> Tools - File watchers
add scss and in argument add --style=compressed
If you are using sassc under Linux (Arch) you could use as Arguments:
-t compressed -m auto $FileNameWithoutExtension$.scss $FileNameWithoutExtension$.min.css
For me, --style compressed, --style compressed and --style=compressed don't work.
I had to add the option -x as an argument.
Like here:
If you want more details this post the following post is where I found this solution: https://stackoverflow.com/a/25579991/9861577.

Resources