Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve the heuristics to detect commented out code #230

Open
sylr opened this issue Jun 8, 2022 · 3 comments
Open

improve the heuristics to detect commented out code #230

sylr opened this issue Jun 8, 2022 · 3 comments

Comments

@sylr
Copy link

sylr commented Jun 8, 2022

Follow up of #6 (comment)

I like to make a distinction between comments and commented code, i.e.:

// This is a comment
if thisiscommentedcode == false {

}

//// This is a comment
//if thisiscommentedcode == true {
//
//}

This way I can grep for commented code without having comments returned and vice versa.

Regards.

@sylr sylr changed the title Don't add a space at begining of comments Don't add a space at beginning of comments Jun 8, 2022
@mvdan
Copy link
Owner

mvdan commented Jun 10, 2022

What are you seeing exactly? Gofumpt will not add any spaces to the code you showed as of the latest master version, and the latest stable version is the same.

$ cat /tmp/f.go
package p

func f() {
	// This is a comment
	if thisiscommentedcode == false {

	}

	//// This is a comment
	//if thisiscommentedcode == true {
	//
	//}
}
$ gofumpt -version
v0.3.1
$ gofumpt /tmp/f.go
package p

func f() {
	// This is a comment
	if thisiscommentedcode == false {
	}

	//// This is a comment
	//if thisiscommentedcode == true {
	//
	//}
}
$ gofumpt -d /tmp/f.go
diff -u /tmp/f.go.orig /tmp/f.go
--- /tmp/f.go.orig	2022-06-10 13:25:25.200203324 +0100
+++ /tmp/f.go	2022-06-10 13:25:25.200203324 +0100
@@ -3,7 +3,6 @@
 func f() {
 	// This is a comment
 	if thisiscommentedcode == false {
-
 	}
 
 	//// This is a comment

@sylr
Copy link
Author

sylr commented Jun 10, 2022

package main

import "fmt"

func main() {
	pwet := true
	fmt.Printf("%b", pwet)
	if pwet {
		//fmt.Printf("%b", pwet)
		return
	}
}
$ ~/git/gofumpt/gofumpt -w .
$ git --no-parger diff
diff --git main.go main.go
index 8f26369..22084d7 100644
--- main.go
+++ main.go
@@ -6,7 +6,7 @@ func main() {
 	pwet := true
 	fmt.Printf("%b", pwet)
 	if pwet {
-		//fmt.Printf("%b", pwet)
+		// fmt.Printf("%b", pwet)
 		return
 	}
 }

@mvdan
Copy link
Owner

mvdan commented Jun 10, 2022

The heuristic is at

gofumpt/format/format.go

Lines 415 to 419 in 8f1392a

r, _ := utf8.DecodeRuneInString(body)
if !unicode.IsLetter(r) && !unicode.IsNumber(r) && !unicode.IsSpace(r) {
// this line could be code like "//{"
continue groupLoop
}
; if any of the lines in a contiguous block of comments matches, then we conservatively treat the entire thing as code, so we don't add any spaces.

In your case, there is just one comment and it doesn't match our heuristic - which is currently "is the first character special?".

I guess one way to make the heuristic a bit more clever is to also treat a comment line as code if it parses as a valid Go statement or declaration. That's not particularly easy to do unfortunately, given that https://pkg.go.dev/go/parser only allows parsing files or expressions, but it is doable.

@mvdan mvdan removed the needs info label Jun 10, 2022
@mvdan mvdan changed the title Don't add a space at beginning of comments improve the heuristics to detect commented out code Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants