Coder Social home page Coder Social logo

sublime-gofmt's Introduction

Sublime Gofmt

This Sublime Text 3 package adds support for running gofmt on your Go source files. It runs on save by default, but that can be disabled. You can run it manually via a palette command (Gofmt: Format this file).

If gofmt encounters errors, the plugin will mark the lines and highlight those errors in your source file.

Goimports

If you want to use goimports instead, open to Sublime Text -> Preferences -> Package Settings -> Gofmt -> Settings - User and enter a config like this:

{
  "cmds": [
    ["goimports", "-e"]
  ]
}

Note that cmds is an array, so you can put multiple commands in there, if you want to run the file through more than one formatter for some reason.

sublime-gofmt's People

Contributors

mitranim avatar noonat avatar samihiltunen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sublime-gofmt's Issues

Error when trying to save file - but it saves the file still

I get the following error when trying to save any go file

error: Traceback (most recent call last):
  File "/home/my_user/.config/sublime-text/Installed Packages/Gofmt.sublime-package/gofmt.py", line 257, in run_formatter
    formatter = Formatter(view)
  File "/home/my_user/.config/sublime-text/Installed Packages/Gofmt.sublime-package/gofmt.py", line 166, in __init__
    self.cmds = [Command(cmd, self.view, self.window) for cmd in cmds]
  File "/home/my_user/.config/sublime-text/Installed Packages/Gofmt.sublime-package/gofmt.py", line 166, in <listcomp>
    self.cmds = [Command(cmd, self.view, self.window) for cmd in cmds]
  File "/home/my_user/.config/sublime-text/Installed Packages/Gofmt.sublime-package/gofmt.py", line 77, in __init__
    self.window)
  File "/home/my_user/.config/sublime-text/Packages/golangconfig/all/golangconfig.py", line 197, in subprocess_info
    value, _ = setting_value(var_name, view=view, window=window)
  File "/home/my_user/.config/sublime-text/Packages/golangconfig/all/golangconfig.py", line 371, in setting_value
    raise e
golangconfig.GoPathNotFoundError: The GOPATH environment variable value "/home/my_user/.local/go
BASH_FUNC_which%%=() {  ( alias;
 eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}" does not exist on the filesystem

Here's the weird thing though, I don't get the error if I leave my terminal's PS1 alone. I'm currently on Fedora 34 XFCE x11 and Sublime Text Build 4107

Here is my Gofmt.sublime-settings

{
  "cmds": [
    ["goimports", "-e"]
  ],
  "format_on_save": true
}

as for the BASH_FUNC_which function in the error, I have no idea where it's getting that from, that's not in my .bashrc

Sublime hangs when gofmt is executed

When I run gofmt with Sublime, either by saving or through the command palette, the entirety of Sublime will hang, presumably waiting for gofmt to execute. I'm using the default Sublime settings.

I'm able to reproduce something that looks like this by running gofmt -e -s in the terminal; it's execution will hang until I send it SIGTERM.

To reproduce,

  • Open a .go file in Sublime
  • Execute gofmt either by saving or with the command palette
  • Observe Sublime hanging
  • pkill subl

Version information:

Sublime Text Build 4121
go version go1.13.8 linux/amd64
sublime-gofmt 0.1.6

Not sure how to get the gofmt version, one doesn't even show up in the binary.

Palette command is missing?

I'm running ST3 build 3126 on macOS 10.12.4. After installing Gofmt (v0.1.1), I can't find the palette command "Gofmt". Restarting ST3 doesn't help. Restarting the computer doesn't help either.

Viewport moves up exactly one line on format

Whenever I format a file (either through saving, or Gofmt: Format this file), the viewport moves up exactly one line. The actual cursor stays where it's supposed to be, but the view simply shifts up.

This happens both with the default gofmt, and with my custom config for goimports:

{
  "cmds": [
    ["goimports", "-e"]
  ],
  "format_on_save": true
}

how to set keybinding?

I would like to set a keyboard shortcut to trigger Gofmt: Format this file.
Is this possible?

AttributeError: 'module' object has no attribute 'subprocess_info

Recently upgraded Sublime 3 to 3.0 Build 3143. Since then, every time I save a .go file I get an exception:

Traceback (most recent call last):
  File "gofmt in /Users/XXXX/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package", line 233, in run_formatter
  File "gofmt in /Users/XXXX/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package", line 144, in __init__
  File "gofmt in /Users/XXXX/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package", line 144, in <listcomp>
  File "gofmt in /Users/XXXX/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package", line 68, in __init__
AttributeError: 'module' object has no attribute 'subprocess_info'

Looks like subprocess_info is expected to be a attribute of golangconfig. Guessing either this component or golangconfig changed during the update and broke this.

Haven't looked much further yet I'm afraid (sorry, no time).

Conflict with search panel and save_on_focus_lost

There is a conflict with the search panel if the setting save_on_focus_lost is enabled, where the search panel will not show up the first time CTRL+F is pressed.

Steps to reproduce:

  1. Set "save_on_focus_lost": true, in Sublime preferences.
  2. Open a go file and make some unsaved changes.
  3. Press CTRL+F.

I think Sublime Text opens the search panel, which means the editor loses focus, which triggers a save, which triggers the gofmt plugin, which closes the search panel somehow.

Pressing CTRL+F a second time will then work, because there are no unsaved changes.

Better view patching that preserves cursor position

Currently the plugin calls view.replace on the entire buffer contents and then restores the scroll position. This preserves the scroll position, but not the cursor position.

There's a different method we can borrow from DisposaBoy/GoSublime: diff the two buffers, and call view.replace only on the changed regions on the view. This tends to preserve both the scroll and the cursor position. I've tested this in mitranim/sublime-rust-fmt and mitranim/sublime-gofmt (my simplified alternative to this plugin), and it seems to work well. Performance seems fine; I tested this on a 5k LoC file and couldn't feel the difference.

Both of my plugins provide this as an option. If this method causes issues for some users, they can opt out. Anyway if you like the idea, I can make a PR.

AttributeError: 'module' object has no attribute 'get_path'

`
Traceback (most recent call last):

File "D:\Program Files\Sublime Text 3\sublime_plugin.py", line 1088, in run_
return self.run(edit)

File "C:\Users\Lenovo\AppData\Roaming\Sublime Text 3\Installed Packages\Golang Tools Integration.sublime-package\gotools_lint.py", line 25, in run

File "C:\Users\Lenovo\AppData\Roaming\Sublime Text 3\Installed Packages\Golang Tools Integration.sublime-package\gotools_lint.py", line 61, in run_golint

File "C:\Users\Lenovo\AppData\Roaming\Sublime Text 3\Installed Packages\Golang Tools Integration.sublime-package\gotools_util.py", line 95, in run

File "C:\Users\Lenovo\AppData\Roaming\SUBLIM1\Packages\GOLANG1\all\golangconfig.py", line 161, in subprocess_info
path, _ = executable_path(executable_name, view=view, window=window)

File "C:\Users\Lenovo\AppData\Roaming\SUBLIM1\Packages\GOLANG1\all\golangconfig.py", line 447, in executable_path
shell, path_dirs = shellenv.get_path()

AttributeError: 'module' object has no attribute 'get_path'
`

Does not work without go in PATH

I do not have go in my PATH and thus get the error:


Traceback (most recent call last):
  File "gofmt in /home/user/.config/sublime-text-3/Installed Packages/Gofmt.sublime-package", line 233, in run_formatter
  File "gofmt in /home/user/.config/sublime-text-3/Installed Packages/Gofmt.sublime-package", line 144, in __init__
  File "gofmt in /home/user/.config/sublime-text-3/Installed Packages/Gofmt.sublime-package", line 144, in <listcomp>
  File "gofmt in /home/user/.config/sublime-text-3/Installed Packages/Gofmt.sublime-package", line 68, in __init__
AttributeError: 'module' object has no attribute 'subprocess_info'

Would be good to have a way to specify, which Go binary to use for the fmt call.
When changing settings to:

{
  "cmds": [
    ["/home/user/bin/gofmt", "-e", "-s"]
  ],
}

the next error is:

Traceback (most recent call last):
  File "gofmt in /home/andreas/.config/sublime-text-3/Installed Packages/Gofmt.sublime-package", line 233, in run_formatter
  File "gofmt in /home/andreas/.config/sublime-text-3/Installed Packages/Gofmt.sublime-package", line 144, in __init__
  File "gofmt in /home/andreas/.config/sublime-text-3/Installed Packages/Gofmt.sublime-package", line 144, in <listcomp>
  File "gofmt in /home/andreas/.config/sublime-text-3/Installed Packages/Gofmt.sublime-package", line 70, in __init__
  File "/home/andreas/.config/sublime-text-3/Packages/golangconfig/all/golangconfig.py", line 227, in subprocess_info
    raise exception
golangconfig.EnvVarError: The following environment variable is currently unset: GOPATH

GOPATH not set error

Got this error after installing this plugin with package control:

  File "/Users/some_user/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 257, in run_formatter
    formatter = Formatter(view)
  File "/Users/some_user/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 166, in __init__
    self.cmds = [Command(cmd, self.view, self.window) for cmd in cmds]
  File "/Users/some_user/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 166, in <listcomp>
    self.cmds = [Command(cmd, self.view, self.window) for cmd in cmds]
  File "/Users/some_user/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 77, in __init__
    self.window)
  File "/Users/some_user/Library/Application Support/Sublime Text 3/Packages/golangconfig/all/golangconfig.py", line 227, in subprocess_info
    raise exception
golangconfig.EnvVarError: The following environment variable is currently unset: GOPATH

The ENV variable is set if I open up a terminal, but I'm guessing this is looking some where else for that variable...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.