vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

`Extract Function` Refactoring Does Not Work When Comments are Present

Open FloatingSunfish opened this issue 3 years ago • 1 comments

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.19 windows/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    • golang.org/x/tools/gopls v0.9.4
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    • 1.71.0
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.35.2
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
Checking configured tools....
GOBIN: undefined
toolsGopath: 
gopath: C:\Users\Admin\go
GOROOT: C:\Program Files\Go
PATH: C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\swigwin;C:\gallery-dl;C:\ffmpeg\bin;C:\platform-tools;C:\Program Files\Go\bin;C:\TDM-GCC-64\bin;C:\Program Files\Git\cmd;C:\Users\Admin\AppData\Local\Programs\Python\Python310\Scripts\;C:\Users\Admin\AppData\Local\Programs\Python\Python310\;C:\Users\Admin\AppData\Local\Microsoft\WindowsApps;;C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\JetBrains\PyCharm Community Edition 2022.2.1\bin;;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.3\bin;;C:\Users\Admin\go\bin;C:\Users\Admin\.dotnet\tools

	go:	C:\Program Files\Go\bin\go.exe: go version go1.19 windows/amd64

	gotests:	C:\Users\Admin\go\bin\gotests.exe	(version: v1.6.0 built with go: go1.19)
	gomodifytags:	C:\Users\Admin\go\bin\gomodifytags.exe	(version: v1.16.0 built with go: go1.19)
	impl:	C:\Users\Admin\go\bin\impl.exe	(version: v1.1.0 built with go: go1.19)
	goplay:	C:\Users\Admin\go\bin\goplay.exe	(version: v1.0.0 built with go: go1.19)
	dlv:	C:\Users\Admin\go\bin\dlv.exe	(version: v1.9.0 built with go: go1.19)
	golangci-lint:	C:\Users\Admin\go\bin\golangci-lint.exe	(version: v1.48.0 built with go: go1.19)
	gopls:	C:\Users\Admin\go\bin\gopls.exe	(version: v0.9.4 built with go: go1.19)

go env
Workspace Folder (playground): e:\_To_Backup\_02_To_Copy_Periodic\ProjectCodes\GoCodes\playground
	set GO111MODULE=
	set GOARCH=amd64
	set GOBIN=
	set GOCACHE=C:\Users\Admin\AppData\Local\go-build
	set GOENV=C:\Users\Admin\AppData\Roaming\go\env
	set GOEXE=.exe
	set GOEXPERIMENT=
	set GOFLAGS=
	set GOHOSTARCH=amd64
	set GOHOSTOS=windows
	set GOINSECURE=
	set GOMODCACHE=C:\Users\Admin\go\pkg\mod
	set GONOPROXY=
	set GONOSUMDB=
	set GOOS=windows
	set GOPATH=C:\Users\Admin\go
	set GOPRIVATE=
	set GOPROXY=https://proxy.golang.org,direct
	set GOROOT=C:\Program Files\Go
	set GOSUMDB=sum.golang.org
	set GOTMPDIR=
	set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
	set GOVCS=
	set GOVERSION=go1.19
	set GCCGO=gccgo
	set GOAMD64=v1
	set AR=ar
	set CC=gcc
	set CXX=g++
	set CGO_ENABLED=1
	set GOMOD=e:\_To_Backup\_02_To_Copy_Periodic\ProjectCodes\GoCodes\playground\go.mod
	set GOWORK=
	set CGO_CFLAGS=-g -O2
	set CGO_CPPFLAGS=
	set CGO_CXXFLAGS=-g -O2
	set CGO_FFLAGS=-g -O2
	set CGO_LDFLAGS=-g -O2
	set PKG_CONFIG=pkg-config
	set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\Admin\AppData\Local\Temp\go-build1366559408=/tmp/go-build -gno-record-gcc-switches

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file. Share all the settings with the go. or ["go"] or gopls prefixes.

"go.formatTool": "gofmt",
"go.lintFlags": [
    "--fast"
],
"go.lintOnSave": "workspace",
"go.lintTool": "golangci-lint",
"go.vetOnSave": "workspace",

Describe the bug

Extract Function works as expected when Comments are not present in the code. However, Extract Function does not work when Comments are present in the code. You simply get the "No refactorings available" message.

Steps to reproduce the behavior:

The below code is inside a larger function. They cannot be extracted to another function (via the Extract Function refactoring) because of the Comments.

Make sure to include the comments when you highlight all the code in each snippet:

Snippet 1:

	// A comment.
	fmt.Println("Hello, World!")

Snippet 2:

	// Another comment.
	fmt.Println("I'm doing something!")
	fmt.Println("Now I'm doing something else!")

Snippet 3:

	// Yet another comment.
	fmt.Println("Hello, World!")
	fmt.Println("Hello, World!")
	fmt.Println("Hello, World!")

	fmt.Println("Hello, World!")
	fmt.Println("Hello, World!")
	fmt.Println("Hello, World!")

Screenshots or recordings

The above code can be pasted inside any function (e.g. the main function). You will not be able to extract them to another function because of the comments.

FloatingSunfish avatar Sep 04 '22 03:09 FloatingSunfish

This issue is being tracked upstream in golang/go#54816

Will leave this open for discovery purposes until it is fixed

suzmue avatar Sep 08 '22 18:09 suzmue

This is included in the gopls v0.10.0-pre.1. Closing since it will be released soon.

hyangah avatar Oct 17 '22 14:10 hyangah