whitespace summary: frequency count of leading/trailing spaces/tabs

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

whitespace summary: frequency count of leading/trailing spaces/tabs

02 Sep 2018, 15:04

- When working on unfamiliar code, it's good to have an idea of what kind of whitespace is used.
- It's also useful to keep leading whitespace consistent, and trailing whitespace non-existent.

Code: Select all

q:: ;files - whitespace summary
;frequency count: leading/trailing spaces/tabs permutations
;vDir1 := A_Desktop "\AutoHotkey_L-master\source"
vDir1 := A_ScriptDir
vDoMsg := 1
vDoMsgL := 0
vDoMsgR := 0
VarSetCapacity(vOutputAll, 1000000*2)
Loop, Files, % vDir1 "\*", F
{
	vPath := A_LoopFileFullPath
	SplitPath, vPath, vName, vDir, vExt, vNameNoExt, vDrive
	FileRead, vText, % vPath
	oArrayL := {base:{__Get:Func("Abs").Bind(0)}} ;AHK basic object with default value of 0
	oArrayR := {base:{__Get:Func("Abs").Bind(0)}} ;AHK basic object with default value of 0
	Loop, Parse, vText, `n, `r
	{
		vTemp := A_LoopField
		vPos1 := RegExMatch(vTemp, "[^ `t]")
		vPos2 := RegExMatch(vTemp, "[ `t](?=[ `t]*$)")
		vWhitespaceL := SubStr(vTemp, 1, vPos1-1)
		vWhitespaceR := vPos2 ? SubStr(vTemp, vPos2) : ""
		if vWhitespaceL
			oArrayL[vWhitespaceL]++
		if vWhitespaceR
			oArrayR[vWhitespaceR]++
		if vDoMsgL && InStr(vWhitespaceL, " ")
			MsgBox, % "[" JEE_StrReplaceST(vWhitespaceL) "][" vTemp "]"
		if vDoMsgR && InStr(vWhitespaceR, " ")
			MsgBox, % "[" vTemp "][" JEE_StrReplaceST(vWhitespaceR) "]"
	}
	vOutput := vName "`r`n`r`n" "start:`r`n"
	for vKey, vValue in oArrayL
		vOutput .= vValue "`t" JEE_StrReplaceST(vKey) "`r`n"
	vOutput .= "`r`n" "end:`r`n"
	for vKey, vValue in oArrayR
		vOutput .= vValue "`t" JEE_StrReplaceST(vKey) "`r`n"
	if vDoMsg
		MsgBox, % vOutput
	vOutputAll .= vOutput "`r`n"
}
Clipboard := vOutputAll
MsgBox, % "done"
return

JEE_StrReplaceST(vText)
{
	vText := StrReplace(vText, " ", "S")
	return StrReplace(vText, "`t", "T")
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
guest3456
Posts: 3328
Joined: 09 Oct 2013, 10:31

Re: whitespace summary: frequency count of leading/trailing spaces/tabs

02 Sep 2018, 19:45

glad you've changed your opinion on leading whitespace (aka indentation). you used to claim how unnecessary indentation was, despite everyone tell you different. at least you are open to new views in some areas ;)

one way projects use to keep leading whitespace (and other formatting) consistent is to use editorconfig:

https://editorconfig.org/

you'll often find .editorconfig files within git repositories which contains these settings, and as along as your editor supports the editorconfig, then it will automatically adjust between spaces/tabs/newlines/etc


Return to “Scripts and Functions”

Who is online

Users browsing this forum: crocodile, Smile_, svArtist and 20 guests