diff --git a/cmd/run.go b/cmd/run.go index 2327a465..f878d3ec 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -26,7 +26,6 @@ import ( "github.com/wakatime/wakatime-cli/pkg/exitcode" "github.com/wakatime/wakatime-cli/pkg/heartbeat" "github.com/wakatime/wakatime-cli/pkg/ini" - "github.com/wakatime/wakatime-cli/pkg/lexer" "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/offline" "github.com/wakatime/wakatime-cli/pkg/vipertools" @@ -69,11 +68,6 @@ func Run(cmd *cobra.Command, v *viper.Viper) { log.Fatalf("failed to setup logging: %s", err) } - // register all custom lexers - if err := lexer.RegisterAll(); err != nil { - log.Fatalf("failed to register custom lexers: %s", err) - } - if v.GetBool("user-agent") { log.Debugln("command: user-agent") diff --git a/go.mod b/go.mod index 94b86d60..71a77466 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 - github.com/alecthomas/chroma/v2 v2.8.0 + github.com/alecthomas/chroma/v2 v2.9.1 github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 github.com/dlclark/regexp2 v1.10.0 github.com/gandarez/go-olson-timezone v0.1.0 @@ -21,9 +21,9 @@ require ( github.com/spf13/viper v1.16.0 github.com/stretchr/testify v1.8.4 go.etcd.io/bbolt v1.3.7 - golang.org/x/crypto v0.12.0 - golang.org/x/net v0.14.0 - golang.org/x/text v0.12.0 + golang.org/x/crypto v0.13.0 + golang.org/x/net v0.15.0 + golang.org/x/text v0.13.0 gopkg.in/ini.v1 v1.67.0 ) @@ -47,17 +47,17 @@ require ( github.com/kr/fs v0.1.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/pelletier/go-toml/v2 v2.0.9 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/spf13/afero v1.9.5 // indirect + github.com/spf13/afero v1.10.0 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/subosito/gotenv v1.4.2 // indirect + github.com/subosito/gotenv v1.6.0 // indirect github.com/yookoala/realpath v1.0.0 // indirect - golang.org/x/sys v0.11.0 // indirect + golang.org/x/sys v0.12.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/alecthomas/chroma/v2 => github.com/gandarez/chroma/v2 v2.8.0-wakatime.1 +replace github.com/alecthomas/chroma/v2 => github.com/gandarez/chroma/v2 v2.9.1-wakatime.1 replace github.com/matishsiao/goInfo => github.com/wakatime/goInfo v0.1.0-wakatime.8 diff --git a/go.sum b/go.sum index f438c3ac..97880908 100644 --- a/go.sum +++ b/go.sum @@ -75,8 +75,8 @@ github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0X github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= -github.com/gandarez/chroma/v2 v2.8.0-wakatime.1 h1:CHVpXUWoYho4HuH0Iu9sL7b5jG93oUrmWPw5Qb5fb2s= -github.com/gandarez/chroma/v2 v2.8.0-wakatime.1/go.mod h1:yrkMI9807G1ROx13fhe1v6PN2DDeaR73L3d+1nmYQtw= +github.com/gandarez/chroma/v2 v2.9.1-wakatime.1 h1:czvEuO+JRT4y2HTTh0REP0p0ebfleH6eY70UI0WF4OQ= +github.com/gandarez/chroma/v2 v2.9.1-wakatime.1/go.mod h1:4TQu7gdfuPjSh76j78ietmqh9LiurGF0EpseFXdKMBw= github.com/gandarez/go-olson-timezone v0.1.0 h1:cDRlHKQE0uC3mJNZyKoQIpAuvQtV8KXwIVj8bDEEyuo= github.com/gandarez/go-olson-timezone v0.1.0/go.mod h1:+yV/cYNjgs2JqdGShznAD4R13r8lKMGR2XlWAJqa5Yo= github.com/gandarez/go-realpath v1.0.0 h1:fhQBRDshH/MZNmDLWM9vbBameK2fxyLr+ctqkRwbHEU= @@ -224,8 +224,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= -github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0= -github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo= @@ -243,8 +243,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/slongfield/pyfmt v0.0.0-20220222012616-ea85ff4c361f h1:Z2cODYsUxQPofhpYRMQVwWz4yUVpHF+vPi+eUdruUYI= github.com/slongfield/pyfmt v0.0.0-20220222012616-ea85ff4c361f/go.mod h1:JqzWyvTuI2X4+9wOHmKSQCYxybB/8j6Ko43qVmXDuZg= -github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM= -github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= +github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY= +github.com/spf13/afero v1.10.0/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA= github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= @@ -266,8 +266,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= -github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/wakatime/goInfo v0.1.0-wakatime.8 h1:MgyeRnCkynEmUxLKXnYUAP5Dd+vhKxhqg6Nx1PdAZy4= github.com/wakatime/goInfo v0.1.0-wakatime.8/go.mod h1:aEt7p9Rvh67BYApmZwNDPpgircTO2kgdmDUoF/1QmwA= github.com/yookoala/realpath v1.0.0 h1:7OA9pj4FZd+oZDsyvXWQvjn5oBdcHRTV44PpdMSuImQ= @@ -296,8 +296,8 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= -golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= -golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= +golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= +golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -368,8 +368,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= -golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= -golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= +golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -431,13 +431,13 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= -golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0= -golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= +golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -447,8 +447,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= -golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/ip_test.go b/ip_test.go index 056b8d72..cb426bfa 100644 --- a/ip_test.go +++ b/ip_test.go @@ -8,9 +8,10 @@ import ( "net/http" "testing" - "github.com/alecthomas/assert" - "github.com/stretchr/testify/require" "github.com/wakatime/wakatime-cli/pkg/api" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type ( diff --git a/pkg/heartbeat/language.go b/pkg/heartbeat/language.go index 8b005b2d..f3dedd35 100644 --- a/pkg/heartbeat/language.go +++ b/pkg/heartbeat/language.go @@ -503,10 +503,10 @@ const ( LanguageFoxPro // LanguageFreefem represents the Freefem programming language. LanguageFreefem - // LangauageFreeMarker represents the FreeMarker programming language. - LangauageFreeMarker - // LangauageFrege represents the Frege programming language. - LangauageFrege + // LanguageFreeMarker represents the FreeMarker programming language. + LanguageFreeMarker + // LanguageFrege represents the Frege programming language. + LanguageFrege // LanguageFStar represents the F* programming language. LanguageFStar // LanguageFuthark represents the Futhark programming language. @@ -531,6 +531,8 @@ const ( LanguageGDNative // LanguageGDScript represents the GDScript programming language. LanguageGDScript + // LanguageGDScript3 represents the GDScript3 programming language. + LanguageGDScript3 // LanguageGEDCOM represents the GEDCOM programming language. LanguageGEDCOM // LanguageGenie represents the Genie programming language. @@ -573,6 +575,8 @@ const ( LanguageGoHTMLTemplate // LanguageGoTextTemplate represents the Go Text Template programming language. LanguageGoTextTemplate + // LanguageGoTemplate represents the Go Template programming language. + LanguageGoTemplate // LanguageGolo represents the Golo programming language. LanguageGolo // LanguageGoodDataCL represents the GoodData-CL programming language. @@ -1816,8 +1820,8 @@ const ( languageFSharpStr = "F#" languageFoxProStr = "FoxPro" languageFreefemStr = "Freefem" - langauageFreeMarkerStr = "FreeMarker" - langauageFregeStr = "Frege" + languageFreeMarkerStr = "FreeMarker" + languageFregeStr = "Frege" languageFutharkStr = "Futhark" languageFStarStr = "F*" languageFStarLiteralStr = "FStar" @@ -1831,6 +1835,7 @@ const ( languageGDBStr = "GDB" languageGDNativeStr = "GDNative" languageGDScriptStr = "GDScript" + languageGDScript3Str = "GDScript3" languageGEDCOMStr = "GEDCOM" languageGenieStr = "Genie" languageGenshiStr = "Genshi" @@ -2365,6 +2370,7 @@ const ( languageFSharpChromaStr = "FSharp" languageEmacsLispChromaStr = "EmacsLisp" languageGoHTMLTemplateChromaStr = "Go HTML Template" + languageGoTemplateChromaStr = "Go Template" languageGoTextTemplateChromaStr = "Go Text Template" languageHxmlChromaStr = "Hxml" languageISCdhcpdChromaStr = "ISCdhcpd" @@ -2901,10 +2907,10 @@ func ParseLanguage(s string) (Language, bool) { return LanguageFoxPro, true case normalizeString(languageFreefemStr): return LanguageFreefem, true - case normalizeString(langauageFreeMarkerStr): - return LangauageFreeMarker, true - case normalizeString(langauageFregeStr): - return LangauageFrege, true + case normalizeString(languageFreeMarkerStr): + return LanguageFreeMarker, true + case normalizeString(languageFregeStr): + return LanguageFrege, true case normalizeString(languageFutharkStr): return LanguageFuthark, true case normalizeString(languageFStarStr): @@ -2931,6 +2937,8 @@ func ParseLanguage(s string) (Language, bool) { return LanguageGDNative, true case normalizeString(languageGDScriptStr): return LanguageGDScript, true + case normalizeString(languageGDScript3Str): + return LanguageGDScript3, true case normalizeString(languageGEDCOMStr): return LanguageGEDCOM, true case normalizeString(languageGenieStr): @@ -3999,9 +4007,9 @@ func ParseLanguageFromChroma(lexerName string) (Language, bool) { return LanguageDNSZone, true case normalizeString(languageEmacsLispChromaStr): return LanguageEmacsLisp, true - case normalizeString(languageGoHTMLTemplateChromaStr): - return LanguageGo, true - case normalizeString(languageGoTextTemplateChromaStr): + case normalizeString(languageGoHTMLTemplateChromaStr), + normalizeString(languageGoTextTemplateChromaStr), + normalizeString(languageGoTemplateChromaStr): return LanguageGo, true case normalizeString(languageFSharpChromaStr): return LanguageFSharp, true @@ -4587,10 +4595,10 @@ func (l Language) String() string { return languageFoxProStr case LanguageFreefem: return languageFreefemStr - case LangauageFreeMarker: - return langauageFreeMarkerStr - case LangauageFrege: - return langauageFregeStr + case LanguageFreeMarker: + return languageFreeMarkerStr + case LanguageFrege: + return languageFregeStr case LanguageFuthark: return languageFutharkStr case LanguageFStar: @@ -4613,6 +4621,8 @@ func (l Language) String() string { return languageGDBStr case LanguageGDScript: return languageGDScriptStr + case LanguageGDScript3: + return languageGDScript3Str case LanguageGEDCOM: return languageGEDCOMStr case LanguageGenie: @@ -4651,7 +4661,7 @@ func (l Language) String() string { return languageGNStr case LanguageGnuplot: return languageGnuplotStr - case LanguageGo, LanguageGoHTMLTemplate, LanguageGoTextTemplate: + case LanguageGo, LanguageGoTemplate, LanguageGoHTMLTemplate, LanguageGoTextTemplate: return languageGoStr case LanguageGolo: return languageGoloStr diff --git a/pkg/heartbeat/language_test.go b/pkg/heartbeat/language_test.go index 36c9e5c2..40eb6914 100644 --- a/pkg/heartbeat/language_test.go +++ b/pkg/heartbeat/language_test.go @@ -262,8 +262,8 @@ func languageTests() map[string]heartbeat.Language { "Fortran Free Form": heartbeat.LanguageFortranFreeForm, "FoxPro": heartbeat.LanguageFoxPro, "Freefem": heartbeat.LanguageFreefem, - "FreeMarker": heartbeat.LangauageFreeMarker, - "Frege": heartbeat.LangauageFrege, + "FreeMarker": heartbeat.LanguageFreeMarker, + "Frege": heartbeat.LanguageFrege, "Futhark": heartbeat.LanguageFuthark, "Game Maker Language": heartbeat.LanguageGameMakerLanguage, "GAML": heartbeat.LanguageGAML, @@ -1067,7 +1067,7 @@ func TestLanguage_StringChroma_AllLexersSupported(t *testing.T) { // Aliases, which match in addition to standard spelling of languages are ignored here. switch config.Name { - case "Go HTML Template", "Go Text Template": + case "Go HTML Template", "Go Text Template", "Go Template": continue case "TypoScriptCssData", "TypoScriptHtmlData": continue diff --git a/pkg/language/language_test.go b/pkg/language/language_test.go index f964d734..d61eb53e 100644 --- a/pkg/language/language_test.go +++ b/pkg/language/language_test.go @@ -6,7 +6,7 @@ import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" "github.com/wakatime/wakatime-cli/pkg/language" - "github.com/wakatime/wakatime-cli/pkg/lexer" + _ "github.com/wakatime/wakatime-cli/pkg/lexer" // force to load all lexers "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -257,9 +257,6 @@ func TestDetect_FSharp_Over_Forth(t *testing.T) { } func TestDetect_ChromaTopLanguagesRetrofit(t *testing.T) { - err := lexer.RegisterAll() - require.NoError(t, err) - tests := map[string]struct { Filepaths []string Expected heartbeat.Language diff --git a/pkg/lexer/actionscript3.go b/pkg/lexer/actionscript3.go index 7e0a41ea..bd0a969e 100644 --- a/pkg/lexer/actionscript3.go +++ b/pkg/lexer/actionscript3.go @@ -4,40 +4,29 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) // nolint:gochecknoglobals var actionscript3AnalyserRe = regexp.MustCompile(`\w+\s*:\s*\w`) -// ActionScript3 lexer. -type ActionScript3 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageActionScript3.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ActionScript3) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if actionscript3AnalyserRe.MatchString(text) { - return 0.3 - } + lexer.SetAnalyser(func(text string) float32 { + if actionscript3AnalyserRe.MatchString(text) { + return 0.3 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (ActionScript3) Name() string { - return heartbeat.LanguageActionScript.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/actionscript3_test.go b/pkg/lexer/actionscript3_test.go old mode 100644 new mode 100755 index 50d676c4..67717fe6 --- a/pkg/lexer/actionscript3_test.go +++ b/pkg/lexer/actionscript3_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestActionScript3_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestActionScript3_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.ActionScript3{}.Lexer() + l := lexers.Get(heartbeat.LanguageActionScript3.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/adl.go b/pkg/lexer/adl.go index 18c485f6..33949707 100644 --- a/pkg/lexer/adl.go +++ b/pkg/lexer/adl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ADL lexer. -type ADL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageADL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ADL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"adl"}, Filenames: []string{"*.adl", "*.adls", "*.adlf", "*.adlx"}, }, @@ -22,10 +29,5 @@ func (l ADL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ADL) Name() string { - return heartbeat.LanguageADL.StringChroma() + )) } diff --git a/pkg/lexer/agda.go b/pkg/lexer/agda.go index 711c616f..dfa69bd3 100644 --- a/pkg/lexer/agda.go +++ b/pkg/lexer/agda.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Agda lexer. -type Agda struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAgda.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Agda) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"agda"}, Filenames: []string{"*.agda"}, MimeTypes: []string{"text/x-agda"}, @@ -23,10 +30,5 @@ func (l Agda) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Agda) Name() string { - return heartbeat.LanguageAgda.StringChroma() + )) } diff --git a/pkg/lexer/aheui.go b/pkg/lexer/aheui.go index a4bf8dda..f8bfde92 100644 --- a/pkg/lexer/aheui.go +++ b/pkg/lexer/aheui.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Aheui lexer. -type Aheui struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAheui.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Aheui) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"aheui"}, Filenames: []string{"*.aheui"}, }, @@ -22,10 +29,5 @@ func (l Aheui) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Aheui) Name() string { - return heartbeat.LanguageAheui.StringChroma() + )) } diff --git a/pkg/lexer/alloy.go b/pkg/lexer/alloy.go index 303fa857..ce4f91a0 100644 --- a/pkg/lexer/alloy.go +++ b/pkg/lexer/alloy.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Alloy lexer. -type Alloy struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAlloy.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Alloy) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"alloy"}, Filenames: []string{"*.als"}, MimeTypes: []string{"text/x-alloy"}, @@ -23,10 +30,5 @@ func (l Alloy) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Alloy) Name() string { - return heartbeat.LanguageAlloy.StringChroma() + )) } diff --git a/pkg/lexer/ambienttalk.go b/pkg/lexer/ambienttalk.go index 5d001a5c..45f895c9 100644 --- a/pkg/lexer/ambienttalk.go +++ b/pkg/lexer/ambienttalk.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// AmbientTalk lexer. -type AmbientTalk struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAmbientTalk.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l AmbientTalk) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"at", "ambienttalk", "ambienttalk/2"}, Filenames: []string{"*.at"}, MimeTypes: []string{"text/x-ambienttalk"}, @@ -23,10 +30,5 @@ func (l AmbientTalk) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (AmbientTalk) Name() string { - return heartbeat.LanguageAmbientTalk.StringChroma() + )) } diff --git a/pkg/lexer/ampl.go b/pkg/lexer/ampl.go index fe08732d..abfdd8b6 100644 --- a/pkg/lexer/ampl.go +++ b/pkg/lexer/ampl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// AMPL lexer. -type AMPL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAMPL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l AMPL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ampl"}, Filenames: []string{"*.run"}, }, @@ -22,10 +29,5 @@ func (l AMPL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (AMPL) Name() string { - return heartbeat.LanguageAMPL.StringChroma() + )) } diff --git a/pkg/lexer/arrow.go b/pkg/lexer/arrow.go index 43e899cc..3f63e131 100644 --- a/pkg/lexer/arrow.go +++ b/pkg/lexer/arrow.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Arrow lexer. -type Arrow struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageArrow.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Arrow) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"arrow"}, Filenames: []string{"*.arw"}, }, @@ -22,10 +29,5 @@ func (l Arrow) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Arrow) Name() string { - return heartbeat.LanguageArrow.StringChroma() + )) } diff --git a/pkg/lexer/aspectj.go b/pkg/lexer/aspectj.go index 7d5c5d0c..8aa8c40b 100644 --- a/pkg/lexer/aspectj.go +++ b/pkg/lexer/aspectj.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// AspectJ lexer. -type AspectJ struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAspectJ.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l AspectJ) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"aspectj"}, Filenames: []string{"*.aj"}, MimeTypes: []string{"text/x-aspectj"}, @@ -23,10 +30,5 @@ func (l AspectJ) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (AspectJ) Name() string { - return heartbeat.LanguageAspectJ.StringChroma() + )) } diff --git a/pkg/lexer/aspxcsharp.go b/pkg/lexer/aspxcsharp.go index 4f9521de..a1362717 100644 --- a/pkg/lexer/aspxcsharp.go +++ b/pkg/lexer/aspxcsharp.go @@ -4,8 +4,10 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -13,26 +15,28 @@ var ( csharpAspxAnalyzerScriptLanguageRe = regexp.MustCompile(`(?i)script[^>]+language=["\']C#`) ) -// AspxCSharp lexer. -type AspxCSharp struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAspxCSharp.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l AspxCSharp) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"aspx-cs"}, Filenames: []string{"*.aspx", "*.asax", "*.ascx", "*.ashx", "*.asmx", "*.axd"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if csharpAspxAnalyzerPageLanguageRe.MatchString(text) { return 0.2 } @@ -42,12 +46,5 @@ func (l AspxCSharp) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (AspxCSharp) Name() string { - return heartbeat.LanguageAspxCSharp.StringChroma() + })) } diff --git a/pkg/lexer/aspxcsharp_test.go b/pkg/lexer/aspxcsharp_test.go old mode 100644 new mode 100755 index b6226e4a..409f5f93 --- a/pkg/lexer/aspxcsharp_test.go +++ b/pkg/lexer/aspxcsharp_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAspxCSharp_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestAspxCSharp_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.AspxCSharp{}.Lexer() + l := lexers.Get(heartbeat.LanguageAspxCSharp.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/aspxvb.go b/pkg/lexer/aspxvb.go index 789e637a..ba1a943f 100644 --- a/pkg/lexer/aspxvb.go +++ b/pkg/lexer/aspxvb.go @@ -4,8 +4,10 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) // nolint:gochecknoglobals @@ -14,26 +16,28 @@ var ( vbAspxAnalyzerScriptLanguageRe = regexp.MustCompile(`(?i)script[^>]+language=["\']vb`) ) -// AspxVBNet lexer. -type AspxVBNet struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAspxVBNet.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l AspxVBNet) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"aspx-vb"}, Filenames: []string{"*.aspx", "*.asax", "*.ascx", "*.ashx", "*.asmx", "*.axd"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if vbAspxAnalyzerPageLanguageRe.MatchString(text) { return 0.2 } @@ -43,12 +47,5 @@ func (l AspxVBNet) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (AspxVBNet) Name() string { - return heartbeat.LanguageAspxVBNet.StringChroma() + })) } diff --git a/pkg/lexer/aspxvb_test.go b/pkg/lexer/aspxvb_test.go old mode 100644 new mode 100755 index b0e79caa..dd895f69 --- a/pkg/lexer/aspxvb_test.go +++ b/pkg/lexer/aspxvb_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAspxVBNet_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestAspxVBNet_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.AspxVBNet{}.Lexer() + l := lexers.Get(heartbeat.LanguageAspxVBNet.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/astro.go b/pkg/lexer/astro.go index d3921b1b..a77ef0c6 100644 --- a/pkg/lexer/astro.go +++ b/pkg/lexer/astro.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Astro lexer. -type Astro struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAstro.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Astro) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"astro"}, Filenames: []string{"*.astro"}, }, @@ -22,10 +29,5 @@ func (l Astro) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Astro) Name() string { - return heartbeat.LanguageAstro.StringChroma() + )) } diff --git a/pkg/lexer/asymptote.go b/pkg/lexer/asymptote.go index 799b1971..8a6ef861 100644 --- a/pkg/lexer/asymptote.go +++ b/pkg/lexer/asymptote.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Asymptote lexer. -type Asymptote struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAsymptote.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Asymptote) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"asy", "asymptote"}, Filenames: []string{"*.asy"}, MimeTypes: []string{"text/x-asymptote"}, @@ -23,10 +30,5 @@ func (l Asymptote) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Asymptote) Name() string { - return heartbeat.LanguageAsymptote.StringChroma() + )) } diff --git a/pkg/lexer/augeas.go b/pkg/lexer/augeas.go index a9cd3a2a..0411795d 100644 --- a/pkg/lexer/augeas.go +++ b/pkg/lexer/augeas.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Augeas lexer. -type Augeas struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageAugeas.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Augeas) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"augeas"}, Filenames: []string{"*.aug"}, }, @@ -22,10 +29,5 @@ func (l Augeas) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Augeas) Name() string { - return heartbeat.LanguageAugeas.StringChroma() + )) } diff --git a/pkg/lexer/bare.go b/pkg/lexer/bare.go index 51fd1386..7ac75e2c 100644 --- a/pkg/lexer/bare.go +++ b/pkg/lexer/bare.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// BARE lexer. -type BARE struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBARE.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l BARE) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"bare"}, Filenames: []string{"*.bare"}, }, @@ -22,10 +29,5 @@ func (l BARE) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (BARE) Name() string { - return heartbeat.LanguageBARE.StringChroma() + )) } diff --git a/pkg/lexer/bbcbasic.go b/pkg/lexer/bbcbasic.go index 88d9fe38..4dfd8527 100644 --- a/pkg/lexer/bbcbasic.go +++ b/pkg/lexer/bbcbasic.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// BBCBasic lexer. -type BBCBasic struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBBCBasic.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l BBCBasic) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"bbcbasic"}, Filenames: []string{"*.bbc"}, }, @@ -24,20 +31,11 @@ func (l BBCBasic) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if strings.HasPrefix(text, "10REM >") || strings.HasPrefix(text, "REM >") { return 0.9 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (BBCBasic) Name() string { - return heartbeat.LanguageBBCBasic.StringChroma() + })) } diff --git a/pkg/lexer/bbcbasic_test.go b/pkg/lexer/bbcbasic_test.go old mode 100644 new mode 100755 index 6d0b9862..cf59a10d --- a/pkg/lexer/bbcbasic_test.go +++ b/pkg/lexer/bbcbasic_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBBCBasic_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestBBCBasic_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.BBCBasic{}.Lexer() + l := lexers.Get(heartbeat.LanguageBBCBasic.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/bbcode.go b/pkg/lexer/bbcode.go index 2f5a244a..12125aba 100644 --- a/pkg/lexer/bbcode.go +++ b/pkg/lexer/bbcode.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// BBCode lexer. -type BBCode struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBBCode.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l BBCode) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"bbcode"}, + Name: language, + Aliases: []string{"bbcode"}, + MimeTypes: []string{"text/x-bbcode"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l BBCode) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (BBCode) Name() string { - return heartbeat.LanguageBBCode.StringChroma() + )) } diff --git a/pkg/lexer/bc.go b/pkg/lexer/bc.go index 7f50c37e..cc748f34 100644 --- a/pkg/lexer/bc.go +++ b/pkg/lexer/bc.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// BC lexer. -type BC struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBC.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l BC) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"bc"}, Filenames: []string{"*.bc"}, }, @@ -22,10 +29,5 @@ func (l BC) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (BC) Name() string { - return heartbeat.LanguageBC.StringChroma() + )) } diff --git a/pkg/lexer/befunge.go b/pkg/lexer/befunge.go index caed4c6e..e7a1f1a8 100644 --- a/pkg/lexer/befunge.go +++ b/pkg/lexer/befunge.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Befunge lexer. -type Befunge struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBefunge.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Befunge) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"befunge"}, Filenames: []string{"*.befunge"}, MimeTypes: []string{"application/x-befunge"}, @@ -23,10 +30,5 @@ func (l Befunge) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Befunge) Name() string { - return heartbeat.LanguageBefunge.StringChroma() + )) } diff --git a/pkg/lexer/blazor.go b/pkg/lexer/blazor.go index c8dcef73..80dbd72e 100644 --- a/pkg/lexer/blazor.go +++ b/pkg/lexer/blazor.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Blazor lexer. -type Blazor struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBlazor.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Blazor) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"blazor"}, Filenames: []string{"*.razor"}, }, @@ -22,10 +29,5 @@ func (l Blazor) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Blazor) Name() string { - return heartbeat.LanguageBlazor.StringChroma() + )) } diff --git a/pkg/lexer/blitzmax.go b/pkg/lexer/blitzmax.go index 360bc2cb..557c0ac9 100644 --- a/pkg/lexer/blitzmax.go +++ b/pkg/lexer/blitzmax.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// BlitzMax lexer. -type BlitzMax struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBlitzMax.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l BlitzMax) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"blitzmax", "bmax"}, Filenames: []string{"*.bmx"}, MimeTypes: []string{"text/x-bmx"}, @@ -23,10 +30,5 @@ func (l BlitzMax) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (BlitzMax) Name() string { - return heartbeat.LanguageBlitzMax.StringChroma() + )) } diff --git a/pkg/lexer/boa.go b/pkg/lexer/boa.go index 716b0fa5..fcacdd17 100644 --- a/pkg/lexer/boa.go +++ b/pkg/lexer/boa.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Boa lexer. -type Boa struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBoa.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Boa) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"boa"}, Filenames: []string{"*.boa"}, }, @@ -22,10 +29,5 @@ func (l Boa) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Boa) Name() string { - return heartbeat.LanguageBoa.StringChroma() + )) } diff --git a/pkg/lexer/boo.go b/pkg/lexer/boo.go index ce53d20d..852e887b 100644 --- a/pkg/lexer/boo.go +++ b/pkg/lexer/boo.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Boo lexer. -type Boo struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBoo.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Boo) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"boo"}, Filenames: []string{"*.boo"}, MimeTypes: []string{"text/x-boo"}, @@ -23,10 +30,5 @@ func (l Boo) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Boo) Name() string { - return heartbeat.LanguageBoo.StringChroma() + )) } diff --git a/pkg/lexer/boogie.go b/pkg/lexer/boogie.go index fb6aaab7..8b25dd37 100644 --- a/pkg/lexer/boogie.go +++ b/pkg/lexer/boogie.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Boogie lexer. -type Boogie struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBoogie.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Boogie) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"boogie"}, Filenames: []string{"*.bpl"}, }, @@ -22,10 +29,5 @@ func (l Boogie) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Boogie) Name() string { - return heartbeat.LanguageBoogie.StringChroma() + )) } diff --git a/pkg/lexer/brainfuck.go b/pkg/lexer/brainfuck.go index a6570794..2bbff0c9 100644 --- a/pkg/lexer/brainfuck.go +++ b/pkg/lexer/brainfuck.go @@ -4,31 +4,22 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// Brainfuck lexer. -type Brainfuck struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBrainfuck.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Brainfuck) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - var ( - ok bool - rgxlexer *chroma.RegexLexer - ) - - if rgxlexer, ok = lexer.(*chroma.RegexLexer); !ok { - return nil - } - - rgxlexer.SetAnalyser(func(text string) float32 { + lexer.SetAnalyser(func(text string) float32 { // it's safe to assume that a program which mostly consists of + - // and < > is brainfuck. var plusMinusCount float64 @@ -63,11 +54,4 @@ func (l Brainfuck) Lexer() chroma.Lexer { return 0 }) - - return rgxlexer -} - -// Name returns the name of the lexer. -func (Brainfuck) Name() string { - return heartbeat.LanguageBrainfuck.StringChroma() } diff --git a/pkg/lexer/brainfuck_test.go b/pkg/lexer/brainfuck_test.go old mode 100644 new mode 100755 index c147602c..9bfec456 --- a/pkg/lexer/brainfuck_test.go +++ b/pkg/lexer/brainfuck_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBrainfuck_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestBrainfuck_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Brainfuck{}.Lexer() + l := lexers.Get(heartbeat.LanguageBrainfuck.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/bst.go b/pkg/lexer/bst.go index 33055d30..a523aeec 100644 --- a/pkg/lexer/bst.go +++ b/pkg/lexer/bst.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// BST lexer. -type BST struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBST.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l BST) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"bst", "bst-pybtex"}, Filenames: []string{"*.bst"}, }, @@ -22,10 +29,5 @@ func (l BST) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (BST) Name() string { - return heartbeat.LanguageBST.StringChroma() + )) } diff --git a/pkg/lexer/bugs.go b/pkg/lexer/bugs.go index dcb4d05e..3b414578 100644 --- a/pkg/lexer/bugs.go +++ b/pkg/lexer/bugs.go @@ -4,43 +4,40 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var bugsAnalyzerRe = regexp.MustCompile(`(?m)^\s*model\s*{`) -// BUGS lexer. -type BUGS struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageBUGS.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l BUGS) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"bugs", "winbugs", "openbugs"}, Filenames: []string{"*.bug"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if bugsAnalyzerRe.MatchString(text) { return 0.7 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (BUGS) Name() string { - return heartbeat.LanguageBUGS.StringChroma() + })) } diff --git a/pkg/lexer/bugs_test.go b/pkg/lexer/bugs_test.go old mode 100644 new mode 100755 index 950c7a01..00aed2bd --- a/pkg/lexer/bugs_test.go +++ b/pkg/lexer/bugs_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBBUGS_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/bugs_basic.bug") assert.NoError(t, err) - l := lexer.BUGS{}.Lexer() + l := lexers.Get(heartbeat.LanguageBUGS.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.7), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/c_test.go b/pkg/lexer/c_test.go old mode 100644 new mode 100755 index f06aecb4..a45f7ded --- a/pkg/lexer/c_test.go +++ b/pkg/lexer/c_test.go @@ -4,9 +4,9 @@ import ( "os" "testing" - "github.com/alecthomas/chroma/v2/lexers" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -30,14 +30,14 @@ func TestC_AnalyseText(t *testing.T) { }, } - l := lexers.Get(heartbeat.LanguageC.StringChroma()) - require.NotNil(t, l) - for name, test := range tests { t.Run(name, func(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) + l := lexers.Get(heartbeat.LanguageC.StringChroma()) + require.NotNil(t, l) + assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) } diff --git a/pkg/lexer/ca65assembler.go b/pkg/lexer/ca65assembler.go index d7fb5ce2..90f71a72 100644 --- a/pkg/lexer/ca65assembler.go +++ b/pkg/lexer/ca65assembler.go @@ -4,45 +4,42 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) // nolint:gochecknoglobals var ca65AnalyserCommentRe = regexp.MustCompile(`(?m)^\s*;`) -// Ca65Assembler lexer. -type Ca65Assembler struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCa65Assembler.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Ca65Assembler) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ca65"}, Filenames: []string{"*.s"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // comments in GAS start with "#". if ca65AnalyserCommentRe.MatchString(text) { return 0.9 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Ca65Assembler) Name() string { - return heartbeat.LanguageCa65Assembler.StringChroma() + })) } diff --git a/pkg/lexer/ca65assembler_test.go b/pkg/lexer/ca65assembler_test.go old mode 100644 new mode 100755 index ba13474e..207fd784 --- a/pkg/lexer/ca65assembler_test.go +++ b/pkg/lexer/ca65assembler_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCa65Assembler_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/ca65assembler_comment.s") assert.NoError(t, err) - l := lexer.Ca65Assembler{}.Lexer() + l := lexers.Get(heartbeat.LanguageCa65Assembler.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.9), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/cadl.go b/pkg/lexer/cadl.go index b2619964..380a8f5e 100644 --- a/pkg/lexer/cadl.go +++ b/pkg/lexer/cadl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CADL lexer. -type CADL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCADL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CADL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cadl"}, Filenames: []string{"*.cadl"}, }, @@ -22,10 +29,5 @@ func (l CADL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CADL) Name() string { - return heartbeat.LanguageCADL.StringChroma() + )) } diff --git a/pkg/lexer/camkes.go b/pkg/lexer/camkes.go index 72a4b656..0b086b01 100644 --- a/pkg/lexer/camkes.go +++ b/pkg/lexer/camkes.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CAmkES lexer. -type CAmkES struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCAmkES.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CAmkES) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"camkes", "idl4"}, Filenames: []string{"*.camkes", "*.idl4"}, }, @@ -22,10 +29,5 @@ func (l CAmkES) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CAmkES) Name() string { - return heartbeat.LanguageCAmkES.StringChroma() + )) } diff --git a/pkg/lexer/capdl.go b/pkg/lexer/capdl.go index df1937dd..1e15b7ed 100644 --- a/pkg/lexer/capdl.go +++ b/pkg/lexer/capdl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CapDL lexer. -type CapDL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCapDL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CapDL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"capdl"}, Filenames: []string{"*.cdl"}, }, @@ -22,10 +29,5 @@ func (l CapDL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CapDL) Name() string { - return heartbeat.LanguageCapDL.StringChroma() + )) } diff --git a/pkg/lexer/cbmbasicv2.go b/pkg/lexer/cbmbasicv2.go index ce842385..1c1da2ca 100644 --- a/pkg/lexer/cbmbasicv2.go +++ b/pkg/lexer/cbmbasicv2.go @@ -4,32 +4,36 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var cbmBasicV2AnalyserRe = regexp.MustCompile(`^\d+`) -// CBMBasicV2 CBM BASIC V2 lexer. -type CBMBasicV2 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCBMBasicV2.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CBMBasicV2) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cbmbas"}, Filenames: []string{"*.bas"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // if it starts with a line number, it shouldn't be a "modern" Basic // like VB.net if cbmBasicV2AnalyserRe.MatchString(text) { @@ -37,12 +41,5 @@ func (l CBMBasicV2) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (CBMBasicV2) Name() string { - return heartbeat.LanguageCBMBasicV2.StringChroma() + })) } diff --git a/pkg/lexer/cbmbasicv2_test.go b/pkg/lexer/cbmbasicv2_test.go old mode 100644 new mode 100755 index 1ed24c37..1301c966 --- a/pkg/lexer/cbmbasicv2_test.go +++ b/pkg/lexer/cbmbasicv2_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCBMBasicV2_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/cbmbasicv2_basic.bas") assert.NoError(t, err) - l := lexer.CBMBasicV2{}.Lexer() + l := lexers.Get(heartbeat.LanguageCBMBasicV2.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.2), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/charmci.go b/pkg/lexer/charmci.go index 5268bca4..99cf160f 100644 --- a/pkg/lexer/charmci.go +++ b/pkg/lexer/charmci.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Charmci lexer. -type Charmci struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCharmci.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Charmci) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"charmci"}, Filenames: []string{"*.ci"}, }, @@ -22,10 +29,5 @@ func (l Charmci) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Charmci) Name() string { - return heartbeat.LanguageCharmci.StringChroma() + )) } diff --git a/pkg/lexer/cirru.go b/pkg/lexer/cirru.go index f75a93f6..0b72978f 100644 --- a/pkg/lexer/cirru.go +++ b/pkg/lexer/cirru.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Cirru lexer. -type Cirru struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCirru.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Cirru) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cirru"}, Filenames: []string{"*.cirru"}, MimeTypes: []string{"text/x-cirru"}, @@ -23,10 +30,5 @@ func (l Cirru) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Cirru) Name() string { - return heartbeat.LanguageCirru.StringChroma() + )) } diff --git a/pkg/lexer/clay.go b/pkg/lexer/clay.go index 0d56dce8..10370f7e 100644 --- a/pkg/lexer/clay.go +++ b/pkg/lexer/clay.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Clay lexer. -type Clay struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageClay.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Clay) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"clay"}, Filenames: []string{"*.clay"}, MimeTypes: []string{"text/x-clay"}, @@ -23,10 +30,5 @@ func (l Clay) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Clay) Name() string { - return heartbeat.LanguageClay.StringChroma() + )) } diff --git a/pkg/lexer/clean.go b/pkg/lexer/clean.go index ff429c5c..302cfba0 100644 --- a/pkg/lexer/clean.go +++ b/pkg/lexer/clean.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Clean lexer. -type Clean struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageClean.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Clean) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"clean"}, Filenames: []string{"*.icl", "*.dcl"}, }, @@ -22,10 +29,5 @@ func (l Clean) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Clean) Name() string { - return heartbeat.LanguageClean.StringChroma() + )) } diff --git a/pkg/lexer/clojurescript.go b/pkg/lexer/clojurescript.go index a5cc7094..0998354b 100644 --- a/pkg/lexer/clojurescript.go +++ b/pkg/lexer/clojurescript.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ClojureScript lexer. -type ClojureScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageClojureScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ClojureScript) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"clojurescript", "cljs"}, Filenames: []string{"*.cljs"}, MimeTypes: []string{"text/x-clojurescript", "application/x-clojurescript"}, @@ -23,10 +30,5 @@ func (l ClojureScript) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ClojureScript) Name() string { - return heartbeat.LanguageClojureScript.StringChroma() + )) } diff --git a/pkg/lexer/cobjdump.go b/pkg/lexer/cobjdump.go index 827104e8..83118621 100644 --- a/pkg/lexer/cobjdump.go +++ b/pkg/lexer/cobjdump.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CObjdump lexer. -type CObjdump struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCObjdump.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CObjdump) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"c-objdump"}, Filenames: []string{"*.c-objdump"}, MimeTypes: []string{"text/x-c-objdump"}, @@ -23,10 +30,5 @@ func (l CObjdump) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CObjdump) Name() string { - return heartbeat.LanguageCObjdump.StringChroma() + )) } diff --git a/pkg/lexer/cobolfree.go b/pkg/lexer/cobolfree.go index 07ef8d9f..706f4741 100644 --- a/pkg/lexer/cobolfree.go +++ b/pkg/lexer/cobolfree.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// COBOLFree lexer. -type COBOLFree struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCOBOLFree.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l COBOLFree) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cobolfree"}, Filenames: []string{"*.cbl", "*.CBL"}, }, @@ -22,10 +29,5 @@ func (l COBOLFree) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (COBOLFree) Name() string { - return heartbeat.LanguageCOBOLFree.StringChroma() + )) } diff --git a/pkg/lexer/coldfusioncfc.go b/pkg/lexer/coldfusioncfc.go index 5c7adf93..2bc6771d 100644 --- a/pkg/lexer/coldfusioncfc.go +++ b/pkg/lexer/coldfusioncfc.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ColdfusionCFC lexer. -type ColdfusionCFC struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageColdfusionCFC.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ColdfusionCFC) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cfc"}, Filenames: []string{"*.cfc"}, }, @@ -22,10 +29,5 @@ func (l ColdfusionCFC) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ColdfusionCFC) Name() string { - return heartbeat.LanguageColdfusionCFC.StringChroma() + )) } diff --git a/pkg/lexer/coldfusionhtml.go b/pkg/lexer/coldfusionhtml.go index 55cf31d9..3e609a48 100644 --- a/pkg/lexer/coldfusionhtml.go +++ b/pkg/lexer/coldfusionhtml.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ColdfusionHTML lexer. -type ColdfusionHTML struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageColdfusionHTML.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ColdfusionHTML) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cfm"}, Filenames: []string{"*.cfm", "*.cfml"}, MimeTypes: []string{"application/x-coldfusion"}, @@ -23,10 +30,5 @@ func (l ColdfusionHTML) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ColdfusionHTML) Name() string { - return heartbeat.LanguageColdfusionHTML.StringChroma() + )) } diff --git a/pkg/lexer/componentpascal.go b/pkg/lexer/componentpascal.go index aae7fc2c..e8af019a 100644 --- a/pkg/lexer/componentpascal.go +++ b/pkg/lexer/componentpascal.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ComponentPascal lexer. -type ComponentPascal struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageComponentPascal.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ComponentPascal) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"componentpascal", "cp"}, Filenames: []string{"*.cp", "*.cps"}, MimeTypes: []string{"text/x-component-pascal"}, @@ -23,10 +30,5 @@ func (l ComponentPascal) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ComponentPascal) Name() string { - return heartbeat.LanguageComponentPascal.StringChroma() + )) } diff --git a/pkg/lexer/coq.go b/pkg/lexer/coq.go index 9f97e369..a36d35f2 100644 --- a/pkg/lexer/coq.go +++ b/pkg/lexer/coq.go @@ -4,37 +4,26 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// Coq lexer. -type Coq struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCoq.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Coq) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if strings.Contains(text, "Qed") && strings.Contains(text, "Proof") { - return 1.0 - } + lexer.SetAnalyser(func(text string) float32 { + if strings.Contains(text, "Qed") && strings.Contains(text, "Proof") { + return 1.0 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Coq) Name() string { - return heartbeat.LanguageCoq.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/coq_test.go b/pkg/lexer/coq_test.go old mode 100644 new mode 100755 index 17cebb06..4f4f339a --- a/pkg/lexer/coq_test.go +++ b/pkg/lexer/coq_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCoq_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/coq_reserved_keyword.v") assert.NoError(t, err) - l := lexer.Coq{}.Lexer() + l := lexers.Get(heartbeat.LanguageCoq.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/cpp_test.go b/pkg/lexer/cpp_test.go old mode 100644 new mode 100755 index 70e5a7a0..59310be2 --- a/pkg/lexer/cpp_test.go +++ b/pkg/lexer/cpp_test.go @@ -4,9 +4,9 @@ import ( "os" "testing" - "github.com/alecthomas/chroma/v2/lexers" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -26,14 +26,14 @@ func TestCpp_AnalyseText(t *testing.T) { }, } - l := lexers.Get(heartbeat.LanguageCPP.StringChroma()) - require.NotNil(t, l) - for name, test := range tests { t.Run(name, func(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) + l := lexers.Get(heartbeat.LanguageCPP.StringChroma()) + require.NotNil(t, l) + assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) } diff --git a/pkg/lexer/cppobjdump.go b/pkg/lexer/cppobjdump.go index 131c9d64..cf42eeb6 100644 --- a/pkg/lexer/cppobjdump.go +++ b/pkg/lexer/cppobjdump.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CppObjdump lexer. -type CppObjdump struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCppObjdump.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CppObjdump) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cpp-objdump", "c++-objdumb", "cxx-objdump"}, Filenames: []string{"*.cpp-objdump", "*.c++-objdump", "*.cxx-objdump"}, MimeTypes: []string{"text/x-cpp-objdump"}, @@ -23,10 +30,5 @@ func (l CppObjdump) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CppObjdump) Name() string { - return heartbeat.LanguageCppObjdump.StringChroma() + )) } diff --git a/pkg/lexer/cpsa.go b/pkg/lexer/cpsa.go index ac3b2831..2cf77cdc 100644 --- a/pkg/lexer/cpsa.go +++ b/pkg/lexer/cpsa.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CPSA lexer. -type CPSA struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCPSA.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CPSA) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cpsa"}, Filenames: []string{"*.cpsa"}, }, @@ -22,10 +29,5 @@ func (l CPSA) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CPSA) Name() string { - return heartbeat.LanguageCPSA.StringChroma() + )) } diff --git a/pkg/lexer/crmsh.go b/pkg/lexer/crmsh.go index 289e7607..9352487c 100644 --- a/pkg/lexer/crmsh.go +++ b/pkg/lexer/crmsh.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Crmsh lexer. -type Crmsh struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCrmsh.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Crmsh) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"crmsh", "pcmk"}, Filenames: []string{"*.crmsh", "*.pcmk"}, }, @@ -22,10 +29,5 @@ func (l Crmsh) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Crmsh) Name() string { - return heartbeat.LanguageCrmsh.StringChroma() + )) } diff --git a/pkg/lexer/croc.go b/pkg/lexer/croc.go index 9ec370e7..abccd028 100644 --- a/pkg/lexer/croc.go +++ b/pkg/lexer/croc.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Croc lexer. -type Croc struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCroc.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Croc) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"croc"}, Filenames: []string{"*.croc"}, MimeTypes: []string{"text/x-crocsrc"}, @@ -23,10 +30,5 @@ func (l Croc) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Croc) Name() string { - return heartbeat.LanguageCroc.StringChroma() + )) } diff --git a/pkg/lexer/crontab.go b/pkg/lexer/crontab.go index 4e9c92ca..469e0fc8 100644 --- a/pkg/lexer/crontab.go +++ b/pkg/lexer/crontab.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Crontab lexer. -type Crontab struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCrontab.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Crontab) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"crontab"}, Filenames: []string{"crontab"}, }, @@ -22,10 +29,5 @@ func (l Crontab) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Crontab) Name() string { - return heartbeat.LanguageCrontab.StringChroma() + )) } diff --git a/pkg/lexer/cryptol.go b/pkg/lexer/cryptol.go index 7aad62d1..1dc66bb2 100644 --- a/pkg/lexer/cryptol.go +++ b/pkg/lexer/cryptol.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Cryptol lexer. -type Cryptol struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCryptol.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Cryptol) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cryptol", "cry"}, Filenames: []string{"*.cry"}, MimeTypes: []string{"text/x-cryptol"}, @@ -23,10 +30,5 @@ func (l Cryptol) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Cryptol) Name() string { - return heartbeat.LanguageCryptol.StringChroma() + )) } diff --git a/pkg/lexer/csounddocument.go b/pkg/lexer/csounddocument.go index f1de5b5d..c9e32e45 100644 --- a/pkg/lexer/csounddocument.go +++ b/pkg/lexer/csounddocument.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CsoundDocument lexer. -type CsoundDocument struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCsoundDocument.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CsoundDocument) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"csound-document", "csound-csd"}, Filenames: []string{"*.csd"}, }, @@ -22,10 +29,5 @@ func (l CsoundDocument) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CsoundDocument) Name() string { - return heartbeat.LanguageCsoundDocument.StringChroma() + )) } diff --git a/pkg/lexer/csoundorchestra.go b/pkg/lexer/csoundorchestra.go index 8d48cd0b..9cabebfb 100644 --- a/pkg/lexer/csoundorchestra.go +++ b/pkg/lexer/csoundorchestra.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CsoundOrchestra lexer. -type CsoundOrchestra struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCsoundOrchestra.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CsoundOrchestra) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"csound", "csound-orc"}, Filenames: []string{"*.orc", "*.udo"}, }, @@ -22,10 +29,5 @@ func (l CsoundOrchestra) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CsoundOrchestra) Name() string { - return heartbeat.LanguageCsoundOrchestra.StringChroma() + )) } diff --git a/pkg/lexer/csoundscore.go b/pkg/lexer/csoundscore.go index 1afcdc51..7ad9fa33 100644 --- a/pkg/lexer/csoundscore.go +++ b/pkg/lexer/csoundscore.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// CsoundScore lexer. -type CsoundScore struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCsoundScore.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CsoundScore) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"csound-score", "csound-sco"}, Filenames: []string{"*.sco"}, }, @@ -22,10 +29,5 @@ func (l CsoundScore) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (CsoundScore) Name() string { - return heartbeat.LanguageCsoundScore.StringChroma() + )) } diff --git a/pkg/lexer/cuda.go b/pkg/lexer/cuda.go index 307f4e02..bd7ca763 100644 --- a/pkg/lexer/cuda.go +++ b/pkg/lexer/cuda.go @@ -2,19 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// CUDA lexer. -type CUDA struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCUDA.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l CUDA) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cuda", "cu"}, Filenames: []string{"*.cu", "*.cuh"}, MimeTypes: []string{"text/x-cuda"}, @@ -24,21 +30,12 @@ func (l CUDA) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { c := lexers.Get(heartbeat.LanguageC.StringChroma()) if c == nil { return 0 } return c.AnalyseText(text) - }) - - return lexer -} - -// Name returns the name of the lexer. -func (CUDA) Name() string { - return heartbeat.LanguageCUDA.StringChroma() + })) } diff --git a/pkg/lexer/cuda_test.go b/pkg/lexer/cuda_test.go old mode 100644 new mode 100755 index d0777d7c..3ed19262 --- a/pkg/lexer/cuda_test.go +++ b/pkg/lexer/cuda_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCUDA_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestCUDA_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.CUDA{}.Lexer() + l := lexers.Get(heartbeat.LanguageCUDA.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/cypher.go b/pkg/lexer/cypher.go index a148a434..0a99f35a 100644 --- a/pkg/lexer/cypher.go +++ b/pkg/lexer/cypher.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Cypher lexer. -type Cypher struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageCypher.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Cypher) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"cypher"}, Filenames: []string{"*.cyp", "*.cypher"}, }, @@ -22,10 +29,5 @@ func (l Cypher) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Cypher) Name() string { - return heartbeat.LanguageCypher.StringChroma() + )) } diff --git a/pkg/lexer/darcspatch.go b/pkg/lexer/darcspatch.go index e31158b7..6b839e03 100644 --- a/pkg/lexer/darcspatch.go +++ b/pkg/lexer/darcspatch.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// DarcsPatch lexer. -type DarcsPatch struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDarcsPatch.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l DarcsPatch) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"dpatch"}, Filenames: []string{"*.dpatch", "*.darcspatch"}, }, @@ -22,10 +29,5 @@ func (l DarcsPatch) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (DarcsPatch) Name() string { - return heartbeat.LanguageDarcsPatch.StringChroma() + )) } diff --git a/pkg/lexer/dasm16.go b/pkg/lexer/dasm16.go index 7fd7d826..2d063a2a 100644 --- a/pkg/lexer/dasm16.go +++ b/pkg/lexer/dasm16.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// DASM16 lexer. -type DASM16 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDASM16.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l DASM16) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"dasm16"}, Filenames: []string{"*.dasm16", "*.dasm"}, MimeTypes: []string{"text/x-dasm16"}, @@ -23,10 +30,5 @@ func (l DASM16) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (DASM16) Name() string { - return heartbeat.LanguageDASM16.StringChroma() + )) } diff --git a/pkg/lexer/debiancontrol.go b/pkg/lexer/debiancontrol.go index bc5d6587..b2762ab4 100644 --- a/pkg/lexer/debiancontrol.go +++ b/pkg/lexer/debiancontrol.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// DebianControlFile lexer. -type DebianControlFile struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDebianControlFile.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l DebianControlFile) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"control", "debcontrol"}, Filenames: []string{"control"}, }, @@ -22,10 +29,5 @@ func (l DebianControlFile) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (DebianControlFile) Name() string { - return heartbeat.LanguageDebianControlFile.StringChroma() + )) } diff --git a/pkg/lexer/delphi.go b/pkg/lexer/delphi.go index 04fcfbe8..2270a094 100644 --- a/pkg/lexer/delphi.go +++ b/pkg/lexer/delphi.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Delphi lexer. -type Delphi struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDelphi.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Delphi) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"delphi", "pas", "pascal", "objectpascal"}, Filenames: []string{"*.pas", "*.dpr"}, MimeTypes: []string{"text/x-pascal"}, @@ -23,10 +30,5 @@ func (l Delphi) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Delphi) Name() string { - return heartbeat.LanguageDelphi.StringChroma() + )) } diff --git a/pkg/lexer/devicetree.go b/pkg/lexer/devicetree.go index 0f92c149..3c08e4bc 100644 --- a/pkg/lexer/devicetree.go +++ b/pkg/lexer/devicetree.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Devicetree lexer. -type Devicetree struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDevicetree.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Devicetree) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"devicetree", "dts"}, Filenames: []string{"*.dts", "*.dtsi"}, MimeTypes: []string{"text/x-c"}, @@ -23,10 +30,5 @@ func (l Devicetree) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Devicetree) Name() string { - return heartbeat.LanguageDevicetree.StringChroma() + )) } diff --git a/pkg/lexer/dg.go b/pkg/lexer/dg.go index e561b686..4d344d50 100644 --- a/pkg/lexer/dg.go +++ b/pkg/lexer/dg.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// DG lexer. -type DG struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDG.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l DG) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"dg"}, Filenames: []string{"*.dg"}, MimeTypes: []string{"text/x-dg"}, @@ -23,10 +30,5 @@ func (l DG) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (DG) Name() string { - return heartbeat.LanguageDG.StringChroma() + )) } diff --git a/pkg/lexer/dobjdump.go b/pkg/lexer/dobjdump.go index efc9de99..51b47882 100644 --- a/pkg/lexer/dobjdump.go +++ b/pkg/lexer/dobjdump.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// DObjdump lexer. -type DObjdump struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDObjdump.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l DObjdump) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"d-objdump"}, Filenames: []string{"*.d-objdump"}, MimeTypes: []string{"text/x-d-objdump"}, @@ -23,10 +30,5 @@ func (l DObjdump) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (DObjdump) Name() string { - return heartbeat.LanguageDObjdump.StringChroma() + )) } diff --git a/pkg/lexer/duel.go b/pkg/lexer/duel.go index 779f4fe5..24e13589 100644 --- a/pkg/lexer/duel.go +++ b/pkg/lexer/duel.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Duel lexer. -type Duel struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDuel.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Duel) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"duel", "jbst", "jsonml+bst"}, Filenames: []string{"*.duel", "*.jbst"}, MimeTypes: []string{"text/x-duel", "text/x-jbst"}, @@ -23,10 +30,5 @@ func (l Duel) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Duel) Name() string { - return heartbeat.LanguageDuel.StringChroma() + )) } diff --git a/pkg/lexer/dylanlid.go b/pkg/lexer/dylanlid.go index b9fce40e..8cfedb69 100644 --- a/pkg/lexer/dylanlid.go +++ b/pkg/lexer/dylanlid.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// DylanLID lexer. -type DylanLID struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDylanLID.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l DylanLID) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"dylan-lid", "lid"}, Filenames: []string{"*.lid", "*.hdp"}, MimeTypes: []string{"text/x-dylan-lid"}, @@ -23,10 +30,5 @@ func (l DylanLID) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (DylanLID) Name() string { - return heartbeat.LanguageDylanLID.StringChroma() + )) } diff --git a/pkg/lexer/dylansession.go b/pkg/lexer/dylansession.go index e2b27875..92cb717a 100644 --- a/pkg/lexer/dylansession.go +++ b/pkg/lexer/dylansession.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// DylanSession lexer. -type DylanSession struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageDylanSession.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l DylanSession) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"dylan-console", "dylan-repl"}, Filenames: []string{"*.dylan-console"}, MimeTypes: []string{"text/x-dylan-console"}, @@ -23,10 +30,5 @@ func (l DylanSession) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (DylanSession) Name() string { - return heartbeat.LanguageDylanSession.StringChroma() + )) } diff --git a/pkg/lexer/earlgrey.go b/pkg/lexer/earlgrey.go index 6d5c0003..b71029f8 100644 --- a/pkg/lexer/earlgrey.go +++ b/pkg/lexer/earlgrey.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// EarlGrey lexer. -type EarlGrey struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageEarlGrey.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l EarlGrey) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"earl-grey", "earlgrey", "eg"}, Filenames: []string{"*.eg"}, MimeTypes: []string{"text/x-earl-grey"}, @@ -23,10 +30,5 @@ func (l EarlGrey) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (EarlGrey) Name() string { - return heartbeat.LanguageEarlGrey.StringChroma() + )) } diff --git a/pkg/lexer/easytrieve.go b/pkg/lexer/easytrieve.go index b6febb3c..d22c0d18 100644 --- a/pkg/lexer/easytrieve.go +++ b/pkg/lexer/easytrieve.go @@ -5,8 +5,10 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -14,15 +16,19 @@ var ( easytrieveAnalyserMacroHeaderRe = regexp.MustCompile(`\s*MACRO`) ) -// Easytrieve lexer. -type Easytrieve struct{} +// nolint:gochecknoinits,gocyclo +func init() { + language := heartbeat.LanguageEasytrieve.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -// nolint: gocyclo -func (l Easytrieve) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"easytrieve"}, Filenames: []string{"*.ezt", "*.mac"}, MimeTypes: []string{"text/x-easytrieve"}, @@ -32,9 +38,7 @@ func (l Easytrieve) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // Perform a structural analysis for basic Easytrieve constructs. var ( result float32 @@ -146,12 +150,5 @@ func (l Easytrieve) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Easytrieve) Name() string { - return heartbeat.LanguageEasytrieve.StringChroma() + })) } diff --git a/pkg/lexer/easytrieve_test.go b/pkg/lexer/easytrieve_test.go old mode 100644 new mode 100755 index 43744b67..e696719c --- a/pkg/lexer/easytrieve_test.go +++ b/pkg/lexer/easytrieve_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestEasytrieve_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestEasytrieve_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Easytrieve{}.Lexer() + l := lexers.Get(heartbeat.LanguageEasytrieve.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/ec.go b/pkg/lexer/ec.go index 6de0eca3..be1c72af 100644 --- a/pkg/lexer/ec.go +++ b/pkg/lexer/ec.go @@ -2,19 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// EC lexer. -type EC struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageEC.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l EC) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ec"}, Filenames: []string{"*.ec", "*.eh"}, MimeTypes: []string{"text/x-echdr", "text/x-ecsrc"}, @@ -24,21 +30,12 @@ func (l EC) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { c := lexers.Get(heartbeat.LanguageC.StringChroma()) if c == nil { return 0 } return c.AnalyseText(text) - }) - - return lexer -} - -// Name returns the name of the lexer. -func (EC) Name() string { - return heartbeat.LanguageEC.StringChroma() + })) } diff --git a/pkg/lexer/ec_test.go b/pkg/lexer/ec_test.go old mode 100644 new mode 100755 index 4128534e..89468226 --- a/pkg/lexer/ec_test.go +++ b/pkg/lexer/ec_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestEC_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestEC_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.EC{}.Lexer() + l := lexers.Get(heartbeat.LanguageEC.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/ecl.go b/pkg/lexer/ecl.go index 64fab649..267b2db4 100644 --- a/pkg/lexer/ecl.go +++ b/pkg/lexer/ecl.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ECL lexer. -type ECL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageECL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ECL) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ecl"}, Filenames: []string{"*.ecl"}, MimeTypes: []string{"application/x-ecl"}, @@ -25,9 +32,7 @@ func (l ECL) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // This is very difficult to guess relative to other business languages. // -> in conjunction with BEGIN/END seems relatively rare though. @@ -46,12 +51,5 @@ func (l ECL) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (ECL) Name() string { - return heartbeat.LanguageECL.StringChroma() + })) } diff --git a/pkg/lexer/ecl_test.go b/pkg/lexer/ecl_test.go old mode 100644 new mode 100755 index 906366e8..352f37ed --- a/pkg/lexer/ecl_test.go +++ b/pkg/lexer/ecl_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestECL_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestECL_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.ECL{}.Lexer() + l := lexers.Get(heartbeat.LanguageECL.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/eiffel.go b/pkg/lexer/eiffel.go index d5d64ee2..d5778e6d 100644 --- a/pkg/lexer/eiffel.go +++ b/pkg/lexer/eiffel.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Eiffel lexer. -type Eiffel struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageEiffel.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Eiffel) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"eiffel"}, Filenames: []string{"*.e"}, MimeTypes: []string{"text/x-eiffel"}, @@ -23,10 +30,5 @@ func (l Eiffel) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Eiffel) Name() string { - return heartbeat.LanguageEiffel.StringChroma() + )) } diff --git a/pkg/lexer/elixiriexsession.go b/pkg/lexer/elixiriexsession.go index d8076ef7..9ef5e8ed 100644 --- a/pkg/lexer/elixiriexsession.go +++ b/pkg/lexer/elixiriexsession.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ElixirIexSsession lexer. -type ElixirIexSsession struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageElixirIexSession.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ElixirIexSsession) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"iex"}, + Name: language, + Aliases: []string{"iex"}, + MimeTypes: []string{"text/x-elixir-shellsession"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l ElixirIexSsession) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ElixirIexSsession) Name() string { - return heartbeat.LanguageElixirIexSession.StringChroma() + )) } diff --git a/pkg/lexer/email.go b/pkg/lexer/email.go index dfadfe9f..f98e2a10 100644 --- a/pkg/lexer/email.go +++ b/pkg/lexer/email.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// EMail lexer. -type EMail struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageEMail.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l EMail) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"email", "eml"}, Filenames: []string{"*.eml"}, MimeTypes: []string{"message/rfc822"}, @@ -23,10 +30,5 @@ func (l EMail) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (EMail) Name() string { - return heartbeat.LanguageEMail.StringChroma() + )) } diff --git a/pkg/lexer/erb.go b/pkg/lexer/erb.go index b5e4baaf..e4191bf7 100644 --- a/pkg/lexer/erb.go +++ b/pkg/lexer/erb.go @@ -4,19 +4,27 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ERB lexer. -type ERB struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageERB.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ERB) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"erb"}, + Name: language, + Aliases: []string{"erb"}, + MimeTypes: []string{"application/x-ruby-templating"}, }, func() chroma.Rules { @@ -24,20 +32,11 @@ func (l ERB) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if strings.Contains(text, "<%") && strings.Contains(text, "%>") { return 0.4 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (ERB) Name() string { - return heartbeat.LanguageERB.StringChroma() + })) } diff --git a/pkg/lexer/erb_test.go b/pkg/lexer/erb_test.go old mode 100644 new mode 100755 index ccaf4619..f87bd176 --- a/pkg/lexer/erb_test.go +++ b/pkg/lexer/erb_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestERB_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/erb_basic.erb") assert.NoError(t, err) - l := lexer.ERB{}.Lexer() + l := lexers.Get(heartbeat.LanguageERB.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.4), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/erlangerlsession.go b/pkg/lexer/erlangerlsession.go index 0610e399..8a7a700f 100644 --- a/pkg/lexer/erlangerlsession.go +++ b/pkg/lexer/erlangerlsession.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ErlangErlSession lexer. -type ErlangErlSession struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageErlangErlSession.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ErlangErlSession) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"erl"}, Filenames: []string{"*.erl-sh"}, MimeTypes: []string{"text/x-erl-shellsession"}, @@ -23,10 +30,5 @@ func (l ErlangErlSession) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ErlangErlSession) Name() string { - return heartbeat.LanguageErlangErlSession.StringChroma() + )) } diff --git a/pkg/lexer/evoque.go b/pkg/lexer/evoque.go index 4f671466..c633ee0b 100644 --- a/pkg/lexer/evoque.go +++ b/pkg/lexer/evoque.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Evoque lexer. -type Evoque struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageEvoque.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Evoque) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"evoque"}, Filenames: []string{"*.evoque"}, MimeTypes: []string{"application/x-evoque"}, @@ -25,21 +32,12 @@ func (l Evoque) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // Evoque templates use $evoque, which is unique. if strings.Contains(text, "$evoque") { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Evoque) Name() string { - return heartbeat.LanguageEvoque.StringChroma() + })) } diff --git a/pkg/lexer/evoque_test.go b/pkg/lexer/evoque_test.go old mode 100644 new mode 100755 index 7412d838..43ceefb9 --- a/pkg/lexer/evoque_test.go +++ b/pkg/lexer/evoque_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestEvoque_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/evoque_basic.evoque") assert.NoError(t, err) - l := lexer.Evoque{}.Lexer() + l := lexers.Get(heartbeat.LanguageEvoque.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/execline.go b/pkg/lexer/execline.go index 96e20dd9..af8499c2 100644 --- a/pkg/lexer/execline.go +++ b/pkg/lexer/execline.go @@ -2,19 +2,26 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/shebang" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Execline lexer. -type Execline struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageExecline.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Execline) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"execline"}, Filenames: []string{"*.exec"}, }, @@ -23,20 +30,11 @@ func (l Execline) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if matched, _ := shebang.MatchString(text, "execlineb"); matched { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Execline) Name() string { - return heartbeat.LanguageExecline.StringChroma() + })) } diff --git a/pkg/lexer/execline_test.go b/pkg/lexer/execline_test.go old mode 100644 new mode 100755 index d35f7d70..ac4fc8db --- a/pkg/lexer/execline_test.go +++ b/pkg/lexer/execline_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestExecline_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/execline_shebang.exec") assert.NoError(t, err) - l := lexer.Execline{}.Lexer() + l := lexers.Get(heartbeat.LanguageExecline.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/ezhil.go b/pkg/lexer/ezhil.go index 35845017..05fe9c12 100644 --- a/pkg/lexer/ezhil.go +++ b/pkg/lexer/ezhil.go @@ -4,20 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ezhilAnalyserRe = regexp.MustCompile(`[u0b80-u0bff]`) -// Ezhil lexer. -type Ezhil struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageEzhil.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Ezhil) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ezhil"}, Filenames: []string{"*.n"}, MimeTypes: []string{"text/x-ezhil"}, @@ -27,9 +34,7 @@ func (l Ezhil) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // this language uses Tamil-script. We'll assume that if there's a // decent amount of Tamil-characters, it's this language. This assumption // is obviously horribly off if someone uses string literals in tamil @@ -39,12 +44,5 @@ func (l Ezhil) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Ezhil) Name() string { - return heartbeat.LanguageEzhil.StringChroma() + })) } diff --git a/pkg/lexer/ezhil_test.go b/pkg/lexer/ezhil_test.go old mode 100644 new mode 100755 index 96b55a95..5c3e6801 --- a/pkg/lexer/ezhil_test.go +++ b/pkg/lexer/ezhil_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestEzhil_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/ezhil_basic.n") assert.NoError(t, err) - l := lexer.Ezhil{}.Lexer() + l := lexers.Get(heartbeat.LanguageEzhil.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.25), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/fancy.go b/pkg/lexer/fancy.go index c459e89d..14485eef 100644 --- a/pkg/lexer/fancy.go +++ b/pkg/lexer/fancy.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Fancy lexer. -type Fancy struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFancy.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Fancy) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"fancy", "fy"}, Filenames: []string{"*.fy", "*.fancypack"}, MimeTypes: []string{"text/x-fancysrc"}, @@ -23,10 +30,5 @@ func (l Fancy) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Fancy) Name() string { - return heartbeat.LanguageFancy.StringChroma() + )) } diff --git a/pkg/lexer/fantom.go b/pkg/lexer/fantom.go index e1b41826..c9cb8db3 100644 --- a/pkg/lexer/fantom.go +++ b/pkg/lexer/fantom.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Fantom lexer. -type Fantom struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFantom.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Fantom) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"fan"}, Filenames: []string{"*.fan"}, MimeTypes: []string{"application/x-fantom"}, @@ -23,10 +30,5 @@ func (l Fantom) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Fantom) Name() string { - return heartbeat.LanguageFantom.StringChroma() + )) } diff --git a/pkg/lexer/felix.go b/pkg/lexer/felix.go index 668a93c0..96315f27 100644 --- a/pkg/lexer/felix.go +++ b/pkg/lexer/felix.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Felix lexer. -type Felix struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFelix.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Felix) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"felix", "flx"}, Filenames: []string{"*.flx", "*.flxh"}, MimeTypes: []string{"text/x-felix"}, @@ -23,10 +30,5 @@ func (l Felix) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Felix) Name() string { - return heartbeat.LanguageFelix.StringChroma() + )) } diff --git a/pkg/lexer/flatline.go b/pkg/lexer/flatline.go index f7683496..e40509ed 100644 --- a/pkg/lexer/flatline.go +++ b/pkg/lexer/flatline.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Flatline lexer. -type Flatline struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFlatline.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Flatline) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"flatline"}, + Name: language, + Aliases: []string{"flatline"}, + MimeTypes: []string{"text/x-flatline"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l Flatline) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Flatline) Name() string { - return heartbeat.LanguageFlatline.StringChroma() + )) } diff --git a/pkg/lexer/floscript.go b/pkg/lexer/floscript.go index f41a742e..ead1cee6 100644 --- a/pkg/lexer/floscript.go +++ b/pkg/lexer/floscript.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// FloScript lexer. -type FloScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFloScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l FloScript) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"floscript", "flo"}, Filenames: []string{"*.flo"}, }, @@ -22,10 +29,5 @@ func (l FloScript) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (FloScript) Name() string { - return heartbeat.LanguageFloScript.StringChroma() + )) } diff --git a/pkg/lexer/forth.go b/pkg/lexer/forth.go index 77a80898..c0f6a4a3 100644 --- a/pkg/lexer/forth.go +++ b/pkg/lexer/forth.go @@ -5,44 +5,33 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) // nolint:gochecknoglobals var forthAnalyzerRe = regexp.MustCompile(`\n:[^\n]+;\n`) -// Forth lexer. -type Forth struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageForth.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Forth) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - text = strings.ReplaceAll(text, "\r\n", "\n") + lexer.SetAnalyser(func(text string) float32 { + text = strings.ReplaceAll(text, "\r\n", "\n") - // Forth uses : COMMAND ; quite a lot in a single line, so we're trying - // to find that. - if forthAnalyzerRe.MatchString(text) { - return 0.3 - } + // Forth uses : COMMAND ; quite a lot in a single line, so we're trying + // to find that. + if forthAnalyzerRe.MatchString(text) { + return 0.3 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Forth) Name() string { - return heartbeat.LanguageForth.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/forth_test.go b/pkg/lexer/forth_test.go old mode 100644 new mode 100755 index a3bbc8da..168f2ec3 --- a/pkg/lexer/forth_test.go +++ b/pkg/lexer/forth_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestForth_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/forth_command.frt") assert.NoError(t, err) - l := lexer.Forth{}.Lexer() + l := lexers.Get(heartbeat.LanguageForth.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.3), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/foxpro.go b/pkg/lexer/foxpro.go index b2c216b6..93fdd12c 100644 --- a/pkg/lexer/foxpro.go +++ b/pkg/lexer/foxpro.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// FoxPro lexer. -type FoxPro struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFoxPro.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l FoxPro) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"foxpro", "vfp", "clipper", "xbase"}, Filenames: []string{"*.PRG", "*.prg"}, }, @@ -22,10 +29,5 @@ func (l FoxPro) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (FoxPro) Name() string { - return heartbeat.LanguageFoxPro.StringChroma() + )) } diff --git a/pkg/lexer/freefem.go b/pkg/lexer/freefem.go index 090b6ec2..e98f4801 100644 --- a/pkg/lexer/freefem.go +++ b/pkg/lexer/freefem.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Freefem lexer. -type Freefem struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFreefem.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Freefem) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"freefem"}, Filenames: []string{"*.edp"}, MimeTypes: []string{"text/x-freefem"}, @@ -23,10 +30,5 @@ func (l Freefem) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Freefem) Name() string { - return heartbeat.LanguageFreefem.StringChroma() + )) } diff --git a/pkg/lexer/fsharp.go b/pkg/lexer/fsharp.go index f9110f09..943d0a20 100644 --- a/pkg/lexer/fsharp.go +++ b/pkg/lexer/fsharp.go @@ -4,45 +4,34 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// FSharp lexer. -type FSharp struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFSharp.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l FSharp) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - // F# doesn't have that many unique features -- |> and <| are weak - // indicators. - var result float32 + lexer.SetAnalyser(func(text string) float32 { + // F# doesn't have that many unique features -- |> and <| are weak + // indicators. + var result float32 - if strings.Contains(text, "|>") { - result += 0.05 - } + if strings.Contains(text, "|>") { + result += 0.05 + } - if strings.Contains(text, "<|") { - result += 0.05 - } + if strings.Contains(text, "<|") { + result += 0.05 + } - return result - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (FSharp) Name() string { - return heartbeat.LanguageFSharp.StringChroma() + return result + }) } diff --git a/pkg/lexer/fsharp_test.go b/pkg/lexer/fsharp_test.go old mode 100644 new mode 100755 index 1a8ae083..4c37bb7d --- a/pkg/lexer/fsharp_test.go +++ b/pkg/lexer/fsharp_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestFSharp_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestFSharp_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.FSharp{}.Lexer() + l := lexers.Get(heartbeat.LanguageFSharp.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/fstar.go b/pkg/lexer/fstar.go index eb4d793e..2ddaf3d2 100644 --- a/pkg/lexer/fstar.go +++ b/pkg/lexer/fstar.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// FStar lexer. -type FStar struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageFStar.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l FStar) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"fstar"}, Filenames: []string{"*.fst", "*.fsti"}, MimeTypes: []string{"text/x-fstar"}, @@ -23,10 +30,5 @@ func (l FStar) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (FStar) Name() string { - return heartbeat.LanguageFStar.StringChroma() + )) } diff --git a/pkg/lexer/gap.go b/pkg/lexer/gap.go index 3dd9c444..528e8a10 100644 --- a/pkg/lexer/gap.go +++ b/pkg/lexer/gap.go @@ -5,8 +5,10 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -16,26 +18,28 @@ var ( `(DeclareRepresentation|Install(GlobalFunction|Method|ImmediateMethod|OtherMethod)|New(Family|Type)|Objectify)`) ) -// Gap lexer. -type Gap struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGap.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Gap) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"gap"}, Filenames: []string{"*.g", "*.gd", "*.gi", "*.gap"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { var result float64 if gapAnalyserDeclarationRe.MatchString(text) { @@ -47,12 +51,5 @@ func (l Gap) Lexer() chroma.Lexer { } return float32(math.Min(result, float64(1.0))) - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Gap) Name() string { - return heartbeat.LanguageGap.StringChroma() + })) } diff --git a/pkg/lexer/gap_test.go b/pkg/lexer/gap_test.go old mode 100644 new mode 100755 index 04719648..21a2b487 --- a/pkg/lexer/gap_test.go +++ b/pkg/lexer/gap_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGap_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestGap_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Gap{}.Lexer() + l := lexers.Get(heartbeat.LanguageGap.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/gas.go b/pkg/lexer/gas.go index 83f3ba6f..b35a8c4f 100644 --- a/pkg/lexer/gas.go +++ b/pkg/lexer/gas.go @@ -4,8 +4,8 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) @@ -14,36 +14,25 @@ var ( gasAnalyzerOtherDirectiveRe = regexp.MustCompile(`(?m)^\.\w+`) ) -// Gas lexer. -type Gas struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGas.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Gas) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if gasAnalyzerDirectiveRe.MatchString(text) { - return 1.0 - } + lexer.SetAnalyser(func(text string) float32 { + if gasAnalyzerDirectiveRe.MatchString(text) { + return 1.0 + } - if gasAnalyzerOtherDirectiveRe.MatchString(text) { - return 0.1 - } + if gasAnalyzerOtherDirectiveRe.MatchString(text) { + return 0.1 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Gas) Name() string { - return heartbeat.LanguageGas.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/gas_test.go b/pkg/lexer/gas_test.go old mode 100644 new mode 100755 index acf3bb81..2865c111 --- a/pkg/lexer/gas_test.go +++ b/pkg/lexer/gas_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGas_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestGas_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Gas{}.Lexer() + l := lexers.Get(heartbeat.LanguageGas.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/gdscript.go b/pkg/lexer/gdscript.go index 6a0f685c..19cf2211 100644 --- a/pkg/lexer/gdscript.go +++ b/pkg/lexer/gdscript.go @@ -5,8 +5,8 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) @@ -16,42 +16,31 @@ var ( gdscriptAnalyserKeyword2Re = regexp.MustCompile(`(var|const|enum|export|signal|tool)`) ) -// GDScript lexer. -type GDScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGDScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l GDScript) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - var result float64 + lexer.SetAnalyser(func(text string) float32 { + var result float64 - if gdscriptAnalyserFuncRe.MatchString(text) { - result += 0.8 - } + if gdscriptAnalyserFuncRe.MatchString(text) { + result += 0.8 + } - if gdscriptAnalyserKeywordRe.MatchString(text) { - result += 0.4 - } + if gdscriptAnalyserKeywordRe.MatchString(text) { + result += 0.4 + } - if gdscriptAnalyserKeyword2Re.MatchString(text) { - result += 0.2 - } + if gdscriptAnalyserKeyword2Re.MatchString(text) { + result += 0.2 + } - return float32(math.Min(result, float64(1.0))) - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (GDScript) Name() string { - return heartbeat.LanguageGDScript.StringChroma() + return float32(math.Min(result, float64(1.0))) + }) } diff --git a/pkg/lexer/gdscript_test.go b/pkg/lexer/gdscript_test.go old mode 100644 new mode 100755 index 44dc9b5d..fa8dfd27 --- a/pkg/lexer/gdscript_test.go +++ b/pkg/lexer/gdscript_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGdSript_AnalyseText(t *testing.T) { @@ -37,7 +39,8 @@ func TestGdSript_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.GDScript{}.Lexer() + l := lexers.Get(heartbeat.LanguageGDScript.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/gettext.go b/pkg/lexer/gettext.go index f77fb5fe..a9d258fe 100644 --- a/pkg/lexer/gettext.go +++ b/pkg/lexer/gettext.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// GettextCatalog lexer. -type GettextCatalog struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGettextCatalog.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l GettextCatalog) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"pot", "po"}, Filenames: []string{"*.pot", "*.po"}, MimeTypes: []string{"application/x-gettext", "text/x-gettext", "text/gettext"}, @@ -23,10 +30,5 @@ func (l GettextCatalog) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (GettextCatalog) Name() string { - return heartbeat.LanguageGettextCatalog.StringChroma() + )) } diff --git a/pkg/lexer/golo.go b/pkg/lexer/golo.go index e361885b..d7405c90 100644 --- a/pkg/lexer/golo.go +++ b/pkg/lexer/golo.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Golo lexer. -type Golo struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGolo.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Golo) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"golo"}, Filenames: []string{"*.golo"}, }, @@ -22,10 +29,5 @@ func (l Golo) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Golo) Name() string { - return heartbeat.LanguageGolo.StringChroma() + )) } diff --git a/pkg/lexer/gooddatacl.go b/pkg/lexer/gooddatacl.go index 608fe342..23336543 100644 --- a/pkg/lexer/gooddatacl.go +++ b/pkg/lexer/gooddatacl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// GoodDataCL lexer. -type GoodDataCL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGoodDataCL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l GoodDataCL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"gooddata-cl"}, Filenames: []string{"*.gdc"}, MimeTypes: []string{"text/x-gooddata-cl"}, @@ -23,10 +30,5 @@ func (l GoodDataCL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (GoodDataCL) Name() string { - return heartbeat.LanguageGoodDataCL.StringChroma() + )) } diff --git a/pkg/lexer/gosu.go b/pkg/lexer/gosu.go index 5f7e0e38..95143425 100644 --- a/pkg/lexer/gosu.go +++ b/pkg/lexer/gosu.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Gosu lexer. -type Gosu struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGosu.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Gosu) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"gosu"}, Filenames: []string{"*.gs", "*.gsx", "*.gsp", "*.vark"}, MimeTypes: []string{"text/x-gosu"}, @@ -23,10 +30,5 @@ func (l Gosu) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Gosu) Name() string { - return heartbeat.LanguageGosu.StringChroma() + )) } diff --git a/pkg/lexer/gosutemplate.go b/pkg/lexer/gosutemplate.go index db1b1e67..696c323e 100644 --- a/pkg/lexer/gosutemplate.go +++ b/pkg/lexer/gosutemplate.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// GosuTemplate lexer. -type GosuTemplate struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGosuTemplate.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l GosuTemplate) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"gst"}, Filenames: []string{"*.gst"}, MimeTypes: []string{"text/x-gosu-template"}, @@ -23,10 +30,5 @@ func (l GosuTemplate) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (GosuTemplate) Name() string { - return heartbeat.LanguageGosuTemplate.StringChroma() + )) } diff --git a/pkg/lexer/groff.go b/pkg/lexer/groff.go index 6ee152b7..c2a769e9 100644 --- a/pkg/lexer/groff.go +++ b/pkg/lexer/groff.go @@ -5,34 +5,25 @@ import ( "unicode" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) // nolint:gochecknoglobals var groffAlphanumericRe = regexp.MustCompile(`^[a-zA-Z0-9]+$`) -// Groff lexer. -type Groff struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageGroff.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Groff) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - var ( - ok bool - rgxlexer *chroma.RegexLexer - ) - - if rgxlexer, ok = lexer.(*chroma.RegexLexer); !ok { - return nil - } - - rgxlexer.SetAnalyser(func(text string) float32 { + lexer.SetAnalyser(func(text string) float32 { if len(text) <= 1 { return 0 } @@ -63,11 +54,4 @@ func (l Groff) Lexer() chroma.Lexer { return 0 }) - - return rgxlexer -} - -// Name returns the name of the lexer. -func (Groff) Name() string { - return heartbeat.LanguageGroff.StringChroma() } diff --git a/pkg/lexer/groff_test.go b/pkg/lexer/groff_test.go old mode 100644 new mode 100755 index 68793c67..b541a4d8 --- a/pkg/lexer/groff_test.go +++ b/pkg/lexer/groff_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGroff_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestGroff_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Groff{}.Lexer() + l := lexers.Get(heartbeat.LanguageGroff.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/haml.go b/pkg/lexer/haml.go index 005f6d3c..c10be9d5 100644 --- a/pkg/lexer/haml.go +++ b/pkg/lexer/haml.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Haml lexer. -type Haml struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageHaml.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Haml) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"haml"}, Filenames: []string{"*.haml"}, MimeTypes: []string{"text/x-haml"}, @@ -23,10 +30,5 @@ func (l Haml) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Haml) Name() string { - return heartbeat.LanguageHaml.StringChroma() + )) } diff --git a/pkg/lexer/hsail.go b/pkg/lexer/hsail.go index fcfd14ea..7a8f6973 100644 --- a/pkg/lexer/hsail.go +++ b/pkg/lexer/hsail.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// HSAIL lexer. -type HSAIL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageHSAIL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l HSAIL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"hsail", "hsa"}, Filenames: []string{"*.hsail"}, MimeTypes: []string{"text/x-hsail"}, @@ -23,10 +30,5 @@ func (l HSAIL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (HSAIL) Name() string { - return heartbeat.LanguageHSAIL.StringChroma() + )) } diff --git a/pkg/lexer/hspec.go b/pkg/lexer/hspec.go index 27050080..d2d1d6fa 100644 --- a/pkg/lexer/hspec.go +++ b/pkg/lexer/hspec.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Hspec lexer. -type Hspec struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageHspec.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Hspec) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"hspec"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l Hspec) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Hspec) Name() string { - return heartbeat.LanguageHspec.StringChroma() + )) } diff --git a/pkg/lexer/html.go b/pkg/lexer/html.go index e70af6d6..48e2a3a2 100644 --- a/pkg/lexer/html.go +++ b/pkg/lexer/html.go @@ -3,37 +3,26 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/doctype" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// HTML lexer. -type HTML struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageHTML.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l HTML) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if matched, _ := doctype.MatchString(text, "html"); matched { - return 0.5 - } + lexer.SetAnalyser(func(text string) float32 { + if matched, _ := doctype.MatchString(text, "html"); matched { + return 0.5 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (HTML) Name() string { - return heartbeat.LanguageHTML.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/html_test.go b/pkg/lexer/html_test.go old mode 100644 new mode 100755 index 0a698aad..69af3161 --- a/pkg/lexer/html_test.go +++ b/pkg/lexer/html_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestHTML_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/html_doctype.html") assert.NoError(t, err) - l := lexer.HTML{}.Lexer() + l := lexers.Get(heartbeat.LanguageHTML.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.5), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/http.go b/pkg/lexer/http.go index 19c6e12e..8d222168 100644 --- a/pkg/lexer/http.go +++ b/pkg/lexer/http.go @@ -4,17 +4,22 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// HTTP lexer. -type HTTP struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageHTTP.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (HTTP) Lexer() chroma.Lexer { - return lexers.HTTP.SetAnalyser(func(text string) float32 { + if lexer == nil { + log.Debugf("lexer %q not found", language) + return + } + + lexers.HTTP.SetAnalyser(func(text string) float32 { if strings.HasPrefix(text, "GET") || strings.HasPrefix(text, "POST") || strings.HasPrefix(text, "PUT") || @@ -29,8 +34,3 @@ func (HTTP) Lexer() chroma.Lexer { return 0 }) } - -// Name returns the name of the lexer. -func (HTTP) Name() string { - return heartbeat.LanguageHTTP.StringChroma() -} diff --git a/pkg/lexer/http_test.go b/pkg/lexer/http_test.go old mode 100644 new mode 100755 index 08f30871..e36dff6f --- a/pkg/lexer/http_test.go +++ b/pkg/lexer/http_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestHTTP_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/http_request.http") assert.NoError(t, err) - l := lexer.HTTP{}.Lexer() + l := lexers.Get(heartbeat.LanguageHTTP.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/hxml.go b/pkg/lexer/hxml.go index 18da24b0..7478bb12 100644 --- a/pkg/lexer/hxml.go +++ b/pkg/lexer/hxml.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Hxml lexer. -type Hxml struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageHxml.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Hxml) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"haxeml", "hxml"}, Filenames: []string{"*.hxml"}, }, @@ -22,10 +29,5 @@ func (l Hxml) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Hxml) Name() string { - return heartbeat.LanguageHxml.StringChroma() + )) } diff --git a/pkg/lexer/hy.go b/pkg/lexer/hy.go index e612e148..17aca985 100644 --- a/pkg/lexer/hy.go +++ b/pkg/lexer/hy.go @@ -4,37 +4,26 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// Hy lexer. -type Hy struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageHy.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Hy) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if strings.Contains(text, "(import ") || strings.Contains(text, "(defn ") { - return 0.9 - } + lexer.SetAnalyser(func(text string) float32 { + if strings.Contains(text, "(import ") || strings.Contains(text, "(defn ") { + return 0.9 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Hy) Name() string { - return heartbeat.LanguageHy.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/hy_test.go b/pkg/lexer/hy_test.go old mode 100644 new mode 100755 index f93dcedd..fc60dac7 --- a/pkg/lexer/hy_test.go +++ b/pkg/lexer/hy_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestHy_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestHy_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Hy{}.Lexer() + l := lexers.Get(heartbeat.LanguageHy.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/hybris.go b/pkg/lexer/hybris.go index 0b610145..a0df0b57 100644 --- a/pkg/lexer/hybris.go +++ b/pkg/lexer/hybris.go @@ -4,20 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var hybrisAnalyserRe = regexp.MustCompile(`\b(?:public|private)\s+method\b`) -// Hybris lexer. -type Hybris struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageHybris.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Hybris) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"hybris", "hy"}, Filenames: []string{"*.hy", "*.hyb"}, MimeTypes: []string{"text/x-hybris", "application/x-hybris"}, @@ -27,9 +34,7 @@ func (l Hybris) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // public method and private method don't seem to be quite common // elsewhere. if hybrisAnalyserRe.MatchString(text) { @@ -37,12 +42,5 @@ func (l Hybris) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Hybris) Name() string { - return heartbeat.LanguageHybris.StringChroma() + })) } diff --git a/pkg/lexer/hybris_test.go b/pkg/lexer/hybris_test.go old mode 100644 new mode 100755 index 8f8ed28e..ca696b55 --- a/pkg/lexer/hybris_test.go +++ b/pkg/lexer/hybris_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestHybris_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestHybris_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Hybris{}.Lexer() + l := lexers.Get(heartbeat.LanguageHybris.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/icon.go b/pkg/lexer/icon.go index 9cf12660..b87deeff 100644 --- a/pkg/lexer/icon.go +++ b/pkg/lexer/icon.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Icon lexer. -type Icon struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageIcon.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Icon) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"icon"}, Filenames: []string{"*.icon", "*.ICON"}, }, @@ -22,10 +29,5 @@ func (l Icon) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Icon) Name() string { - return heartbeat.LanguageIcon.StringChroma() + )) } diff --git a/pkg/lexer/ida.go b/pkg/lexer/ida.go index 4de585ae..015589c8 100644 --- a/pkg/lexer/ida.go +++ b/pkg/lexer/ida.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// IDA lexer. -type IDA struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageIDA.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l IDA) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"IDA Pro", "IDA Free"}, Filenames: []string{"*.i64", "*.idb"}, MimeTypes: []string{"text/x-ida"}, @@ -23,10 +30,5 @@ func (l IDA) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (IDA) Name() string { - return heartbeat.LanguageIDA.StringChroma() + )) } diff --git a/pkg/lexer/idl.go b/pkg/lexer/idl.go index 0c46e56a..95a8683c 100644 --- a/pkg/lexer/idl.go +++ b/pkg/lexer/idl.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// IDL lexer. -type IDL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageIDL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l IDL) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"idl"}, Filenames: []string{"*.pro"}, MimeTypes: []string{"text/idl"}, @@ -25,9 +32,7 @@ func (l IDL) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // endelse seems to be unique to IDL, endswitch is rare at least. var result float32 @@ -40,12 +45,5 @@ func (l IDL) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (IDL) Name() string { - return heartbeat.LanguageIDL.StringChroma() + })) } diff --git a/pkg/lexer/idl_test.go b/pkg/lexer/idl_test.go old mode 100644 new mode 100755 index 900ec0c7..e6ea4b4d --- a/pkg/lexer/idl_test.go +++ b/pkg/lexer/idl_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestIdl_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestIdl_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.IDL{}.Lexer() + l := lexers.Get(heartbeat.LanguageIDL.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/inform6.go b/pkg/lexer/inform6.go index fc44c632..18c66c53 100644 --- a/pkg/lexer/inform6.go +++ b/pkg/lexer/inform6.go @@ -4,20 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var inform6AnalyserRe = regexp.MustCompile(`(?i)\borigsource\b`) -// Inform6 lexer. -type Inform6 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageInform6.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Inform6) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"inform6", "i6"}, Filenames: []string{"*.inf"}, }, @@ -26,9 +33,7 @@ func (l Inform6) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // We try to find a keyword which seem relatively common, unfortunately // there is a decent overlap with Smalltalk keywords otherwise here. if inform6AnalyserRe.MatchString(text) { @@ -36,12 +41,5 @@ func (l Inform6) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Inform6) Name() string { - return heartbeat.LanguageInform6.StringChroma() + })) } diff --git a/pkg/lexer/inform6_test.go b/pkg/lexer/inform6_test.go old mode 100644 new mode 100755 index 207bdb71..90c89f05 --- a/pkg/lexer/inform6_test.go +++ b/pkg/lexer/inform6_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestInform6_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/inform6_basic.inf") assert.NoError(t, err) - l := lexer.Inform6{}.Lexer() + l := lexers.Get(heartbeat.LanguageInform6.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.05), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/inform6template.go b/pkg/lexer/inform6template.go index 09fc7290..e5e49bac 100644 --- a/pkg/lexer/inform6template.go +++ b/pkg/lexer/inform6template.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Inform6Template lexer. -type Inform6Template struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageInform6Template.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Inform6Template) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"i6t"}, Filenames: []string{"*.i6t"}, }, @@ -22,10 +29,5 @@ func (l Inform6Template) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Inform6Template) Name() string { - return heartbeat.LanguageInform6Template.StringChroma() + )) } diff --git a/pkg/lexer/inform7.go b/pkg/lexer/inform7.go index ac51d48c..cef7e7bd 100644 --- a/pkg/lexer/inform7.go +++ b/pkg/lexer/inform7.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Inform7 lexer. -type Inform7 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageInform7.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Inform7) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"inform7", "i7"}, Filenames: []string{"*.ni", "*.i7x"}, }, @@ -22,10 +29,5 @@ func (l Inform7) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Inform7) Name() string { - return heartbeat.LanguageInform7.StringChroma() + )) } diff --git a/pkg/lexer/ini.go b/pkg/lexer/ini.go index 1d02ca1a..cce3dd58 100644 --- a/pkg/lexer/ini.go +++ b/pkg/lexer/ini.go @@ -4,42 +4,31 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// INI lexer. -type INI struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageINI.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l INI) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - npos := strings.Count(text, "\n") - if npos < 3 { - return 0 - } - - if text[0] == '[' && text[npos-1] == ']' { - return 1 - } - + lexer.SetAnalyser(func(text string) float32 { + npos := strings.Count(text, "\n") + if npos < 3 { return 0 - }) + } - return lexer - } - - return nil -} + if text[0] == '[' && text[npos-1] == ']' { + return 1 + } -// Name returns the name of the lexer. -func (INI) Name() string { - return heartbeat.LanguageINI.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/ini_test.go b/pkg/lexer/ini_test.go old mode 100644 new mode 100755 index eb9612a8..ccf15e8c --- a/pkg/lexer/ini_test.go +++ b/pkg/lexer/ini_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestIni_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestIni_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.INI{}.Lexer() + l := lexers.Get(heartbeat.LanguageINI.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/ioke.go b/pkg/lexer/ioke.go index cdfb4b53..0805f252 100644 --- a/pkg/lexer/ioke.go +++ b/pkg/lexer/ioke.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Ioke lexer. -type Ioke struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageIoke.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Ioke) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ioke", "ik"}, Filenames: []string{"*.ik"}, MimeTypes: []string{"text/x-iokesrc"}, @@ -23,10 +30,5 @@ func (l Ioke) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Ioke) Name() string { - return heartbeat.LanguageIoke.StringChroma() + )) } diff --git a/pkg/lexer/irclogs.go b/pkg/lexer/irclogs.go index 4d60b307..d4ee045e 100644 --- a/pkg/lexer/irclogs.go +++ b/pkg/lexer/irclogs.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// IRCLogs lexer. -type IRCLogs struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageIRCLogs.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l IRCLogs) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"irc"}, Filenames: []string{"*.weechatlog"}, MimeTypes: []string{"text/x-irclog"}, @@ -23,10 +30,5 @@ func (l IRCLogs) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (IRCLogs) Name() string { - return heartbeat.LanguageIRCLogs.StringChroma() + )) } diff --git a/pkg/lexer/isabelle.go b/pkg/lexer/isabelle.go index faf4df6a..d4e51563 100644 --- a/pkg/lexer/isabelle.go +++ b/pkg/lexer/isabelle.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Isabelle lexer. -type Isabelle struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageIsabelle.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Isabelle) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"isabelle"}, Filenames: []string{"*.thy"}, MimeTypes: []string{"text/x-isabelle"}, @@ -23,10 +30,5 @@ func (l Isabelle) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Isabelle) Name() string { - return heartbeat.LanguageIsabelle.StringChroma() + )) } diff --git a/pkg/lexer/jags.go b/pkg/lexer/jags.go index a8560117..a92e00af 100644 --- a/pkg/lexer/jags.go +++ b/pkg/lexer/jags.go @@ -4,8 +4,10 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -14,26 +16,28 @@ var ( jagsAnalyserVarRe = regexp.MustCompile(`(?m)^\s*var`) ) -// JAGS lexer. -type JAGS struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageJAGS.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l JAGS) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"jags"}, Filenames: []string{"*.jag", "*.bug"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if jagsAnalyserModelRe.MatchString(text) { if jagsAnalyserDataRe.MatchString(text) { return 0.9 @@ -47,12 +51,5 @@ func (l JAGS) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (JAGS) Name() string { - return heartbeat.LanguageJAGS.StringChroma() + })) } diff --git a/pkg/lexer/jags_test.go b/pkg/lexer/jags_test.go old mode 100644 new mode 100755 index 6a0ee086..8f681101 --- a/pkg/lexer/jags_test.go +++ b/pkg/lexer/jags_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestJAGS_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestJAGS_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.JAGS{}.Lexer() + l := lexers.Get(heartbeat.LanguageJAGS.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/jasmin.go b/pkg/lexer/jasmin.go index eb438076..ff877d9e 100644 --- a/pkg/lexer/jasmin.go +++ b/pkg/lexer/jasmin.go @@ -5,8 +5,10 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -16,14 +18,19 @@ var ( `(?m)^\s*\.(attribute|bytecode|debug|deprecated|enclosing|inner|interface|limit|set|signature|stack)\b`) ) -// Jasmin lexer. -type Jasmin struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageJasmin.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Jasmin) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"jasmin", "jasminxt"}, Filenames: []string{"*.j"}, }, @@ -32,9 +39,7 @@ func (l Jasmin) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { var result float64 if jasminAnalyserClassRe.MatchString(text) { @@ -50,12 +55,5 @@ func (l Jasmin) Lexer() chroma.Lexer { } return float32(math.Min(result, float64(1.0))) - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Jasmin) Name() string { - return heartbeat.LanguageJasmin.StringChroma() + })) } diff --git a/pkg/lexer/jasmin_test.go b/pkg/lexer/jasmin_test.go old mode 100644 new mode 100755 index 61fb6914..31a9aa12 --- a/pkg/lexer/jasmin_test.go +++ b/pkg/lexer/jasmin_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestJasmin_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestJasmin_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Jasmin{}.Lexer() + l := lexers.Get(heartbeat.LanguageJasmin.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/jcl.go b/pkg/lexer/jcl.go index 6212fc5d..b24407d6 100644 --- a/pkg/lexer/jcl.go +++ b/pkg/lexer/jcl.go @@ -5,20 +5,27 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var jclAnalyserJobHeaderRe = regexp.MustCompile(`(?i)^//[a-z#$@][a-z0-9#$@]{0,7}\s+job(\s+.*)?$`) -// JCL lexer. -type JCL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageJCL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l JCL) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q not found", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"jcl"}, Filenames: []string{"*.jcl"}, MimeTypes: []string{"text/x-jcl"}, @@ -28,9 +35,7 @@ func (l JCL) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // Recognize JCL job by header. lines := strings.Split(text, "\n") if len(lines) == 0 { @@ -42,12 +47,5 @@ func (l JCL) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (JCL) Name() string { - return heartbeat.LanguageJCL.StringChroma() + })) } diff --git a/pkg/lexer/jcl_test.go b/pkg/lexer/jcl_test.go old mode 100644 new mode 100755 index 273c1575..76f82da4 --- a/pkg/lexer/jcl_test.go +++ b/pkg/lexer/jcl_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestJCL_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/jcl_job_header.jcl") assert.NoError(t, err) - l := lexer.JCL{}.Lexer() + l := lexers.Get(heartbeat.LanguageJCL.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/jsgf.go b/pkg/lexer/jsgf.go index a1debdcb..ecb49e3a 100644 --- a/pkg/lexer/jsgf.go +++ b/pkg/lexer/jsgf.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// JSGF lexer. -type JSGF struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageJSGF.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l JSGF) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"jsgf"}, Filenames: []string{"*.jsgf"}, MimeTypes: []string{"application/jsgf", "application/x-jsgf", "text/jsgf"}, @@ -23,10 +30,5 @@ func (l JSGF) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (JSGF) Name() string { - return heartbeat.LanguageJSGF.StringChroma() + )) } diff --git a/pkg/lexer/jsonld.go b/pkg/lexer/jsonld.go index 157c2730..7a204224 100644 --- a/pkg/lexer/jsonld.go +++ b/pkg/lexer/jsonld.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// JSONLD lexer. -type JSONLD struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageJSONLD.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l JSONLD) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"jsonld", "json-ld"}, Filenames: []string{"*.jsonld"}, MimeTypes: []string{"application/ld+json"}, @@ -23,10 +30,5 @@ func (l JSONLD) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (JSONLD) Name() string { - return heartbeat.LanguageJSONLD.StringChroma() + )) } diff --git a/pkg/lexer/jsp.go b/pkg/lexer/jsp.go index cd03dca6..5b74bf1d 100644 --- a/pkg/lexer/jsp.go +++ b/pkg/lexer/jsp.go @@ -4,20 +4,26 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/xml" "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// JSP lexer. -type JSP struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageJSP.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l JSP) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q not found", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"jsp"}, Filenames: []string{"*.jsp"}, MimeTypes: []string{"application/x-jsp"}, @@ -27,9 +33,7 @@ func (l JSP) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { var result float32 java := lexers.Get(heartbeat.LanguageJava.StringChroma()) @@ -46,12 +50,5 @@ func (l JSP) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (JSP) Name() string { - return heartbeat.LanguageJSP.StringChroma() + })) } diff --git a/pkg/lexer/jsp_test.go b/pkg/lexer/jsp_test.go old mode 100644 new mode 100755 index 1f994dfa..2fd2d9bd --- a/pkg/lexer/jsp_test.go +++ b/pkg/lexer/jsp_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestJSP_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/jsp_basic.jsp") assert.NoError(t, err) - l := lexer.JSP{}.Lexer() + l := lexers.Get(heartbeat.LanguageJSP.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.49), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/juliaconsole.go b/pkg/lexer/juliaconsole.go index 1e9bfae1..971270b4 100644 --- a/pkg/lexer/juliaconsole.go +++ b/pkg/lexer/juliaconsole.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// JuliaConsole lexer. -type JuliaConsole struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageJuliaConsole.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l JuliaConsole) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"jlcon"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l JuliaConsole) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (JuliaConsole) Name() string { - return heartbeat.LanguageJuliaConsole.StringChroma() + )) } diff --git a/pkg/lexer/juttle.go b/pkg/lexer/juttle.go index b3df0f8f..0f773e81 100644 --- a/pkg/lexer/juttle.go +++ b/pkg/lexer/juttle.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Juttle lexer. -type Juttle struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageJuttle.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Juttle) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"juttle"}, Filenames: []string{"*.juttle"}, MimeTypes: []string{"application/juttle", "application/x-juttle", "text/x-juttle", "text/juttle"}, @@ -23,10 +30,5 @@ func (l Juttle) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Juttle) Name() string { - return heartbeat.LanguageJuttle.StringChroma() + )) } diff --git a/pkg/lexer/kal.go b/pkg/lexer/kal.go index 8a303598..cc6bc36f 100644 --- a/pkg/lexer/kal.go +++ b/pkg/lexer/kal.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Kal lexer. -type Kal struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageKal.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Kal) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"kal"}, Filenames: []string{"*.kal"}, MimeTypes: []string{"text/kal", "application/kal"}, @@ -23,10 +30,5 @@ func (l Kal) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Kal) Name() string { - return heartbeat.LanguageKal.StringChroma() + )) } diff --git a/pkg/lexer/kconfig.go b/pkg/lexer/kconfig.go index 48870158..2cf63a0e 100644 --- a/pkg/lexer/kconfig.go +++ b/pkg/lexer/kconfig.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Kconfig lexer. -type Kconfig struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageKconfig.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Kconfig) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"kconfig", "menuconfig", "linux-config", "kernel-config"}, Filenames: []string{"Kconfig*", "*Config.in*", "external.in*", "standard-modules.in"}, MimeTypes: []string{"text/x-kconfig"}, @@ -23,10 +30,5 @@ func (l Kconfig) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Kconfig) Name() string { - return heartbeat.LanguageKconfig.StringChroma() + )) } diff --git a/pkg/lexer/kernellog.go b/pkg/lexer/kernellog.go index 97898066..2f0daf4f 100644 --- a/pkg/lexer/kernellog.go +++ b/pkg/lexer/kernellog.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// KernelLog lexer. -type KernelLog struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageKernelLog.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l KernelLog) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"kmsg", "dmesg"}, Filenames: []string{"*.kmsg", "*.dmesg"}, }, @@ -22,10 +29,5 @@ func (l KernelLog) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (KernelLog) Name() string { - return heartbeat.LanguageKernelLog.StringChroma() + )) } diff --git a/pkg/lexer/koka.go b/pkg/lexer/koka.go index bb575932..17b8940c 100644 --- a/pkg/lexer/koka.go +++ b/pkg/lexer/koka.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Koka lexer. -type Koka struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageKoka.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Koka) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"koka"}, Filenames: []string{"*.kk", "*.kki"}, MimeTypes: []string{"text/x-koka"}, @@ -23,10 +30,5 @@ func (l Koka) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Koka) Name() string { - return heartbeat.LanguageKoka.StringChroma() + )) } diff --git a/pkg/lexer/lasso.go b/pkg/lexer/lasso.go index f7f87396..19fac6f1 100644 --- a/pkg/lexer/lasso.go +++ b/pkg/lexer/lasso.go @@ -5,8 +5,10 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -14,37 +16,30 @@ var ( lassoAnalyserLocalRe = regexp.MustCompile(`(?i)local\(`) ) -// Lasso lexer. -type Lasso struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLasso.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Lasso) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{ - "lasso", - "lassoscript", - }, - Filenames: []string{ - "*.lasso", - "*.lasso[89]", - }, - AliasFilenames: []string{ - "*.incl", - "*.inc", - "*.las", - }, - MimeTypes: []string{"text/x-lasso"}, + Name: language, + Aliases: []string{"lasso", "lassoscript"}, + Filenames: []string{"*.lasso", "*.lasso[89]"}, + AliasFilenames: []string{"*.incl", "*.inc", "*.las"}, + MimeTypes: []string{"text/x-lasso"}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { var result float32 if strings.Contains(text, "bin/lasso9") { @@ -60,12 +55,5 @@ func (l Lasso) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Lasso) Name() string { - return heartbeat.LanguageLasso.StringChroma() + })) } diff --git a/pkg/lexer/lasso_test.go b/pkg/lexer/lasso_test.go old mode 100644 new mode 100755 index 0a45ae7c..4318118d --- a/pkg/lexer/lasso_test.go +++ b/pkg/lexer/lasso_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestLasso_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestLasso_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Lasso{}.Lexer() + l := lexers.Get(heartbeat.LanguageLasso.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/lean.go b/pkg/lexer/lean.go index 07438fde..c7c8ac39 100644 --- a/pkg/lexer/lean.go +++ b/pkg/lexer/lean.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Lean lexer. -type Lean struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLean.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Lean) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"lean"}, Filenames: []string{"*.lean"}, MimeTypes: []string{"text/x-lean"}, @@ -23,10 +30,5 @@ func (l Lean) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Lean) Name() string { - return heartbeat.LanguageLean.StringChroma() + )) } diff --git a/pkg/lexer/less.go b/pkg/lexer/less.go index 3dead9c6..634d9e75 100644 --- a/pkg/lexer/less.go +++ b/pkg/lexer/less.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Less lexer. -type Less struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLess.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Less) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"less"}, Filenames: []string{"*.less"}, MimeTypes: []string{"text/x-less-css"}, @@ -23,10 +30,5 @@ func (l Less) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Less) Name() string { - return heartbeat.LanguageLess.StringChroma() + )) } diff --git a/pkg/lexer/lexer.go b/pkg/lexer/lexer.go deleted file mode 100644 index b2654b9b..00000000 --- a/pkg/lexer/lexer.go +++ /dev/null @@ -1,319 +0,0 @@ -package lexer - -import ( - "fmt" - - "github.com/alecthomas/chroma/v2" - l "github.com/alecthomas/chroma/v2/lexers" -) - -// Lexer is an interface that can be implemented by lexers to register them. -type Lexer interface { - Lexer() chroma.Lexer - Name() string -} - -// RegisterAll registers all custom lexers. -func RegisterAll() error { - var lexers = []Lexer{ - ADL{}, - AMPL{}, - ActionScript3{}, - Agda{}, - Aheui{}, - Alloy{}, - AmbientTalk{}, - Arrow{}, - AspectJ{}, - AspxCSharp{}, - AspxVBNet{}, - Astro{}, - Asymptote{}, - Augeas{}, - BARE{}, - BBCBasic{}, - BBCode{}, - BC{}, - BST{}, - BUGS{}, - Befunge{}, - Blazor{}, - BlitzMax{}, - Boa{}, - Boo{}, - Boogie{}, - Brainfuck{}, - CADL{}, - CAmkES{}, - CBMBasicV2{}, - COBOLFree{}, - CObjdump{}, - CPSA{}, - CUDA{}, - Ca65Assembler{}, - CapDL{}, - Charmci{}, - Cirru{}, - Clay{}, - Clean{}, - ClojureScript{}, - ColdfusionCFC{}, - ColdfusionHTML{}, - ComponentPascal{}, - Coq{}, - CppObjdump{}, - Crmsh{}, - Croc{}, - Crontab{}, - Cryptol{}, - CsoundDocument{}, - CsoundOrchestra{}, - CsoundScore{}, - Cypher{}, - DASM16{}, - DG{}, - DObjdump{}, - DarcsPatch{}, - DebianControlFile{}, - Delphi{}, - Devicetree{}, - Duel{}, - DylanLID{}, - DylanSession{}, - EC{}, - ECL{}, - EMail{}, - ERB{}, - EarlGrey{}, - Easytrieve{}, - Eiffel{}, - ElixirIexSsession{}, - ErlangErlSession{}, - Evoque{}, - Execline{}, - Ezhil{}, - FSharp{}, - FStar{}, - Fancy{}, - Fantom{}, - Felix{}, - Flatline{}, - FloScript{}, - Forth{}, - FoxPro{}, - Freefem{}, - GDScript{}, - Gap{}, - Gas{}, - GettextCatalog{}, - Golo{}, - GoodDataCL{}, - Gosu{}, - GosuTemplate{}, - Groff{}, - HSAIL{}, - HTML{}, - HTTP{}, - Haml{}, - Hspec{}, - Hxml{}, - Hy{}, - Hybris{}, - IDL{}, - INI{}, - IRCLogs{}, - Icon{}, - IDA{}, - Inform6{}, - Inform6Template{}, - Inform7{}, - Ioke{}, - Isabelle{}, - JAGS{}, - JCL{}, - JSGF{}, - JSONLD{}, - JSP{}, - Jasmin{}, - JuliaConsole{}, - Juttle{}, - Kal{}, - Kconfig{}, - KernelLog{}, - Koka{}, - LLVMMIR{}, - LLVMMIRBODY{}, - LSL{}, - Lasso{}, - Lean{}, - Less{}, - Limbo{}, - Liquid{}, - LiterateAgda{}, - LiterateCryptol{}, - LiterateHaskell{}, - LiterateIdris{}, - LiveScript{}, - Logos{}, - Logtalk{}, - MAQL{}, - MIME{}, - MOOCode{}, - MQL{}, - MSDOSSession{}, - MXML{}, - Makefile{}, - Marko{}, - Mask{}, - Matlab{}, - MatlabSession{}, - MiniD{}, - MiniScript{}, - Modelica{}, - Modula2{}, - Mojo{}, - Monkey{}, - Monte{}, - MoonScript{}, - Mosel{}, - MozPreprocHash{}, - MozPreprocPercent{}, - Mscgen{}, - MuPAD{}, - Mustache{}, - NASM{}, - NASMObjdump{}, - NCL{}, - NSIS{}, - Nemerle{}, - NesC{}, - NewLisp{}, - Nit{}, - Notmuch{}, - Nushell{}, - NuSMV{}, - NumPy{}, - Objdump{}, - ObjectiveC{}, - ObjectiveCPP{}, - ObjectiveJ{}, - Ooc{}, - Opa{}, - OpenEdgeABL{}, - PEG{}, - POVRay{}, - Pan{}, - ParaSail{}, - Pawn{}, - Perl{}, - Perl6{}, - Pike{}, - Pointless{}, - PostgresConsole{}, - PowerShellSession{}, - Praat{}, - Processing{}, - Prolog{}, - PsyShPHP{}, - Pug{}, - PyPyLog{}, - Python{}, - Python2{}, - Python2Traceback{}, - PythonConsole{}, - PythonTraceback{}, - QBasic{}, - QVTO{}, - R{}, - RConsole{}, - REBOL{}, - RHTML{}, - RNGCompact{}, - RPMSpec{}, - RQL{}, - RSL{}, - RagelEmbedded{}, - RawToken{}, - Razor{}, - Rd{}, - ReScript{}, - Red{}, - Redcode{}, - ResourceBundle{}, - Ride{}, - RoboconfGraph{}, - RoboconfInstances{}, - RobotFramework{}, - RubyIRBSession{}, - SARL{}, - SSP{}, - SWIG{}, - Scaml{}, - Scdoc{}, - ShExC{}, - Shen{}, - Silver{}, - Singularity{}, - SketchDrawing{}, - Slash{}, - Slim{}, - Slint{}, - Slurm{}, - Smali{}, - SmartGameFormat{}, - Snowball{}, - SourcesList{}, - Sqlite3con{}, - Stan{}, - Stata{}, - SublimeTextConfig{}, - SuperCollider{}, - TADS3{}, - TAP{}, - TASM{}, - TNT{}, - TcshSession{}, - Tea{}, - TeraTerm{}, - Tiddler{}, - Todotxt{}, - TrafficScript{}, - TransactSQL{}, - Treetop{}, - Turtle{}, - USD{}, - Ucode{}, - Unicon{}, - UrbiScript{}, - VBNet{}, - VBScript{}, - VCL{}, - VCLSnippets{}, - VCTreeStatus{}, - VGL{}, - Velocity{}, - Verilog{}, - WDiff{}, - WebIDL{}, - X10{}, - XAML{}, - XML{}, - XQuery{}, - XSLT{}, - Xtend{}, - Xtlang{}, - Zeek{}, - Zephir{}, - } - - for _, lexer := range lexers { - found := lexer.Lexer() - if found == nil { - return fmt.Errorf("%q lexer not found", lexer.Name()) - } - - _ = l.Register(lexer.Lexer()) - } - - return nil -} diff --git a/pkg/lexer/lexer_test.go b/pkg/lexer/lexer_test.go deleted file mode 100644 index c66a2327..00000000 --- a/pkg/lexer/lexer_test.go +++ /dev/null @@ -1,313 +0,0 @@ -package lexer_test - -import ( - "testing" - - "github.com/wakatime/wakatime-cli/pkg/heartbeat" - - "github.com/alecthomas/chroma/v2/lexers" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestChromaLanguagesRegister(t *testing.T) { - tests := map[string]heartbeat.Language{ - "actionscript 3": heartbeat.LanguageActionScript3, - "adl": heartbeat.LanguageADL, - "agda": heartbeat.LanguageAgda, - "aheui": heartbeat.LanguageAheui, - "alloy": heartbeat.LanguageAlloy, - "ambienttalk": heartbeat.LanguageAmbientTalk, - "ampl": heartbeat.LanguageAMPL, - "arrow": heartbeat.LanguageArrow, - "aspectj": heartbeat.LanguageAspectJ, - "aspx-cs": heartbeat.LanguageAspxCSharp, - "aspx-vb": heartbeat.LanguageAspxVBNet, - "asymptote": heartbeat.LanguageAsymptote, - "augeas": heartbeat.LanguageAugeas, - "bare": heartbeat.LanguageBARE, - "bbc basic": heartbeat.LanguageBBCBasic, - "bbcode": heartbeat.LanguageBBCode, - "bc": heartbeat.LanguageBC, - "befunge": heartbeat.LanguageBefunge, - "blazor": heartbeat.LanguageBlazor, - "blitzmax": heartbeat.LanguageBlitzMax, - "boa": heartbeat.LanguageBoa, - "boo": heartbeat.LanguageBoo, - "boogie": heartbeat.LanguageBoogie, - "brainfuck": heartbeat.LanguageBrainfuck, - "bst": heartbeat.LanguageBST, - "bugs": heartbeat.LanguageBUGS, - "c-objdump": heartbeat.LanguageCObjdump, - "ca65 assembler": heartbeat.LanguageCa65Assembler, - "cadl": heartbeat.LanguageCADL, - "camkes": heartbeat.LanguageCAmkES, - "capdl": heartbeat.LanguageCapDL, - "cbm basic v2": heartbeat.LanguageCBMBasicV2, - "charmci": heartbeat.LanguageCharmci, - "cirru": heartbeat.LanguageCirru, - "clay": heartbeat.LanguageClay, - "clean": heartbeat.LanguageClean, - "clojurescript": heartbeat.LanguageClojureScript, - "cobolfree": heartbeat.LanguageCOBOLFree, - "coldfusion cfc": heartbeat.LanguageColdfusionCFC, - "coldfusion html": heartbeat.LanguageColdfusionHTML, - "component pascal": heartbeat.LanguageComponentPascal, - "coq": heartbeat.LanguageCoq, - "cpp-objdump": heartbeat.LanguageCppObjdump, - "cpsa": heartbeat.LanguageCPSA, - "crmsh": heartbeat.LanguageCrmsh, - "croc": heartbeat.LanguageCroc, - "crontab": heartbeat.LanguageCrontab, - "cryptol": heartbeat.LanguageCryptol, - "csound document": heartbeat.LanguageCsoundDocument, - "csound orchestra": heartbeat.LanguageCsoundOrchestra, - "csound score": heartbeat.LanguageCsoundScore, - "cuda": heartbeat.LanguageCUDA, - "cypher": heartbeat.LanguageCypher, - "d-objdump": heartbeat.LanguageDObjdump, - "darcs patch": heartbeat.LanguageDarcsPatch, - "dasm16": heartbeat.LanguageDASM16, - "debian control file": heartbeat.LanguageDebianControlFile, - "debian sourcelist": heartbeat.LanguageSourcesList, - "delphi": heartbeat.LanguageDelphi, - "devicetree": heartbeat.LanguageDevicetree, - "dg": heartbeat.LanguageDG, - "duel": heartbeat.LanguageDuel, - "dylan session": heartbeat.LanguageDylanSession, - "dylanlid": heartbeat.LanguageDylanLID, - "e-mail": heartbeat.LanguageEMail, - "earl grey": heartbeat.LanguageEarlGrey, - "easytrieve": heartbeat.LanguageEasytrieve, - "ec": heartbeat.LanguageEC, - "ecl": heartbeat.LanguageECL, - "eiffel": heartbeat.LanguageEiffel, - "elixir iex session": heartbeat.LanguageElixirIexSession, - "embedded ragel": heartbeat.LanguageRagelEmbedded, - "erb": heartbeat.LanguageERB, - "erlang erl session": heartbeat.LanguageErlangErlSession, - "evoque": heartbeat.LanguageEvoque, - "execline": heartbeat.LanguageExecline, - "ezhil": heartbeat.LanguageEzhil, - "fancy": heartbeat.LanguageFancy, - "fantom": heartbeat.LanguageFantom, - "felix": heartbeat.LanguageFelix, - "flatline": heartbeat.LanguageFlatline, - "floscript": heartbeat.LanguageFloScript, - "forth": heartbeat.LanguageForth, - "foxpro": heartbeat.LanguageFoxPro, - "freefem": heartbeat.LanguageFreefem, - "fsharp": heartbeat.LanguageFSharp, - "fstar": heartbeat.LanguageFStar, - "gap": heartbeat.LanguageGap, - "gas": heartbeat.LanguageGas, - "gdscript": heartbeat.LanguageGDScript, - "gettext catalog": heartbeat.LanguageGettextCatalog, - "golo": heartbeat.LanguageGolo, - "gooddata-cl": heartbeat.LanguageGoodDataCL, - "gosu": heartbeat.LanguageGosu, - "gosu template": heartbeat.LanguageGosuTemplate, - "groff": heartbeat.LanguageGroff, - "haml": heartbeat.LanguageHaml, - "hsail": heartbeat.LanguageHSAIL, - "hspec": heartbeat.LanguageHspec, - "html": heartbeat.LanguageHTML, - "http": heartbeat.LanguageHTTP, - "hxml": heartbeat.LanguageHxml, - "hy": heartbeat.LanguageHy, - "hybris": heartbeat.LanguageHybris, - "icon": heartbeat.LanguageIcon, - "idl": heartbeat.LanguageIDL, - "inform 6": heartbeat.LanguageInform6, - "inform 6 template": heartbeat.LanguageInform6Template, - "inform 7": heartbeat.LanguageInform7, - "ini": heartbeat.LanguageINI, - "ioke": heartbeat.LanguageIoke, - "irc logs": heartbeat.LanguageIRCLogs, - "isabelle": heartbeat.LanguageIsabelle, - "jags": heartbeat.LanguageJAGS, - "jasmin": heartbeat.LanguageJasmin, - "java server page": heartbeat.LanguageJSP, - "jcl": heartbeat.LanguageJCL, - "jsgf": heartbeat.LanguageJSGF, - "json-ld": heartbeat.LanguageJSONLD, - "julia console": heartbeat.LanguageJuliaConsole, - "juttle": heartbeat.LanguageJuttle, - "kal": heartbeat.LanguageKal, - "kconfig": heartbeat.LanguageKconfig, - "kernel log": heartbeat.LanguageKernelLog, - "koka": heartbeat.LanguageKoka, - "lasso": heartbeat.LanguageLasso, - "lean": heartbeat.LanguageLean, - "lesscss": heartbeat.LanguageLess, - "limbo": heartbeat.LanguageLimbo, - "liquid": heartbeat.LanguageLiquid, - "literate agda": heartbeat.LanguageLiterateAgda, - "literate cryptol": heartbeat.LanguageLiterateCryptol, - "literate haskell": heartbeat.LanguageLiterateHaskell, - "literate idris": heartbeat.LanguageLiterateIdris, - "livescript": heartbeat.LanguageLiveScript, - "llvm-mir": heartbeat.LanguageLLVMMIR, - "llvm-mir body": heartbeat.LanguageLLVMMIRBody, - "logos": heartbeat.LanguageLogos, - "logtalk": heartbeat.LanguageLogtalk, - "lsl": heartbeat.LanguageLSL, - "makefile": heartbeat.LanguageMakefile, - "maql": heartbeat.LanguageMAQL, - "marko": heartbeat.LanguageMarko, - "mask": heartbeat.LanguageMask, - "matlab": heartbeat.LanguageMatlab, - "matlab session": heartbeat.LanguageMatlabSession, - "mime": heartbeat.LanguageMIME, - "minid": heartbeat.LanguageMiniD, - "miniscript": heartbeat.LanguageMiniScript, - "modelica": heartbeat.LanguageModelica, - "modula-2": heartbeat.LanguageModula2, - "monkey": heartbeat.LanguageMonkey, - "monte": heartbeat.LanguageMonte, - "moocode": heartbeat.LanguageMOOCode, - "moonscript": heartbeat.LanguageMoonScript, - "mosel": heartbeat.LanguageMosel, - "mozhashpreproc": heartbeat.LanguageMozPreprocHash, - "mozpercentpreproc": heartbeat.LanguageMozPreprocPercent, - "mql": heartbeat.LanguageMQL, - "mscgen": heartbeat.LanguageMscgen, - "msdos session": heartbeat.LanguageMSDOSSession, - "mupad": heartbeat.LanguageMuPAD, - "mustache": heartbeat.LanguageMustache, - "mxml": heartbeat.LanguageMXML, - "nasm": heartbeat.LanguageNASM, - "ncl": heartbeat.LanguageNCL, - "nemerle": heartbeat.LanguageNemerle, - "nesc": heartbeat.LanguageNesC, - "newlisp": heartbeat.LanguageNewLisp, - "nit": heartbeat.LanguageNit, - "notmuch": heartbeat.LanguageNotmuch, - "nsis": heartbeat.LanguageNSIS, - "numpy": heartbeat.LanguageNumPy, - "nusmv": heartbeat.LanguageNuSMV, - "objdump": heartbeat.LanguageObjdump, - "objdump-nasm": heartbeat.LanguageNASMObjdump, - "objective-c": heartbeat.LanguageObjectiveC, - "objective-c++": heartbeat.LanguageObjectiveCPP, - "objective-j": heartbeat.LanguageObjectiveJ, - "ooc": heartbeat.LanguageOoc, - "opa": heartbeat.LanguageOpa, - "openedge abl": heartbeat.LanguageOpenEdgeABL, - "pan": heartbeat.LanguagePan, - "parasail": heartbeat.LanguageParaSail, - "pawn": heartbeat.LanguagePawn, - "peg": heartbeat.LanguagePEG, - "perl": heartbeat.LanguagePerl, - "perl6": heartbeat.LanguagePerl6, - "pike": heartbeat.LanguagePike, - "pointless": heartbeat.LanguagePointless, - "postgresql console (psql)": heartbeat.LanguagePostgresConsole, - "povray": heartbeat.LanguagePOVRay, - "powershell session": heartbeat.LanguagePowerShellSession, - "praat": heartbeat.LanguagePraat, - "processing": heartbeat.LanguageProcessing, - "prolog": heartbeat.LanguageProlog, - "psysh console session for php": heartbeat.LanguagePsyShPHP, - "pug": heartbeat.LanguagePug, - "pypy log": heartbeat.LanguagePyPyLog, - "python": heartbeat.LanguagePython, - "python 2": heartbeat.LanguagePython2, - "python 2.x traceback": heartbeat.LanguagePython2Traceback, - "python console session": heartbeat.LanguagePythonConsole, - "python traceback": heartbeat.LanguagePythonTraceback, - "qbasic": heartbeat.LanguageQBasic, - "qvto": heartbeat.LanguageQVTO, - "r": heartbeat.LanguageR, - "raw token data": heartbeat.LanguageRawToken, - "razor": heartbeat.LanguageRazor, - "rconsole": heartbeat.LanguageRConsole, - "rd": heartbeat.LanguageRd, - "rebol": heartbeat.LanguageREBOL, - "red": heartbeat.LanguageRed, - "redcode": heartbeat.LanguageRedcode, - "relax-ng compact": heartbeat.LanguageRNGCompact, - "rescript": heartbeat.LanguageReScript, - "resourcebundle": heartbeat.LanguageResourceBundle, - "rhtml": heartbeat.LanguageRHTML, - "ride": heartbeat.LanguageRide, - "roboconf graph": heartbeat.LanguageRoboconfGraph, - "roboconf instances": heartbeat.LanguageRoboconfInstances, - "robotframework": heartbeat.LanguageRobotFramework, - "rpmspec": heartbeat.LanguageRPMSpec, - "rql": heartbeat.LanguageRQL, - "rsl": heartbeat.LanguageRSL, - "ruby irb session": heartbeat.LanguageRubyIRBSession, - "sarl": heartbeat.LanguageSARL, - "scalate server page": heartbeat.LanguageSSP, - "scaml": heartbeat.LanguageScaml, - "scdoc": heartbeat.LanguageScdoc, - "shen": heartbeat.LanguageShen, - "shexc": heartbeat.LanguageShExC, - "silver": heartbeat.LanguageSilver, - "singularity": heartbeat.LanguageSingularity, - "sketch drawing": heartbeat.LanguageSketchDrawing, - "slash": heartbeat.LanguageSlash, - "slim": heartbeat.LanguageSlim, - "slint": heartbeat.LanguageSlint, - "slurm": heartbeat.LanguageSlurm, - "smali": heartbeat.LanguageSmali, - "smartgameformat": heartbeat.LanguageSmartGameFormat, - "snowball": heartbeat.LanguageSnowball, - "sqlite3con": heartbeat.LanguageSqlite3con, - "stan": heartbeat.LanguageStan, - "stata": heartbeat.LanguageStata, - "sublime text config": heartbeat.LanguageSublimeTextConfig, - "supercollider": heartbeat.LanguageSuperCollider, - "swig": heartbeat.LanguageSWIG, - "tads 3": heartbeat.LanguageTADS3, - "tap": heartbeat.LanguageTAP, - "tasm": heartbeat.LanguageTASM, - "tcsh session": heartbeat.LanguageTcshSession, - "tea": heartbeat.LanguageTea, - "tera term macro": heartbeat.LanguageTeraTerm, - "tiddler": heartbeat.LanguageTiddler, - "todotxt": heartbeat.LanguageTodotxt, - "trafficscript": heartbeat.LanguageTrafficScript, - "transact-sql": heartbeat.LanguageTransactSQL, - "treetop": heartbeat.LanguageTreetop, - "turtle": heartbeat.LanguageTurtle, - "typographic number theory": heartbeat.LanguageTNT, - "ucode": heartbeat.LanguageUcode, - "unicon": heartbeat.LanguageUnicon, - "urbiscript": heartbeat.LanguageUrbiScript, - "usd": heartbeat.LanguageUSD, - "vb.net": heartbeat.LanguageVBNet, - "vbscript": heartbeat.LanguageVBScript, - "vcl": heartbeat.LanguageVCL, - "vclsnippets": heartbeat.LanguageVCLSnippets, - "vctreestatus": heartbeat.LanguageVCTreeStatus, - "velocity": heartbeat.LanguageVelocity, - "verilog": heartbeat.LanguageVerilog, - "vgl": heartbeat.LanguageVGL, - "wdiff": heartbeat.LanguageWDiff, - "web idl": heartbeat.LanguageWebIDL, - "x10": heartbeat.LanguageX10, - "xaml": heartbeat.LanguageXAML, - "xml": heartbeat.LanguageXML, - "xquery": heartbeat.LanguageXQuery, - "xslt": heartbeat.LanguageXSLT, - "xtend": heartbeat.LanguageXtend, - "xtlang": heartbeat.LanguageXtlang, - "zeek": heartbeat.LanguageZeek, - "zephir": heartbeat.LanguageZephir, - } - - for name, test := range tests { - t.Run(name, func(t *testing.T) { - language := test.StringChroma() - require.NotEqual(t, heartbeat.LanguageUnknown.String(), language) - - lexer := lexers.Get(language) - if lexer != nil { - // assert lexer is registered but analyser don't - assert.Nil(t, lexer.Config().Analyse) - } - }) - } -} diff --git a/pkg/lexer/limbo.go b/pkg/lexer/limbo.go index 9b6ca657..0fbea928 100644 --- a/pkg/lexer/limbo.go +++ b/pkg/lexer/limbo.go @@ -4,20 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var limboAnalyzerRe = regexp.MustCompile(`(?m)^implement \w+;`) -// Limbo lexer. -type Limbo struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLimbo.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Limbo) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"limbo"}, Filenames: []string{"*.b"}, MimeTypes: []string{"text/limbo"}, @@ -27,21 +34,12 @@ func (l Limbo) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // Any limbo module implements something if limboAnalyzerRe.MatchString(text) { return 0.7 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Limbo) Name() string { - return heartbeat.LanguageLimbo.StringChroma() + })) } diff --git a/pkg/lexer/limbo_test.go b/pkg/lexer/limbo_test.go old mode 100644 new mode 100755 index 7d586b9b..8605583a --- a/pkg/lexer/limbo_test.go +++ b/pkg/lexer/limbo_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestLimbo_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/limbo_basic.b") assert.NoError(t, err) - l := lexer.Limbo{}.Lexer() + l := lexers.Get(heartbeat.LanguageLimbo.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.7), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/liquid.go b/pkg/lexer/liquid.go index 00228bb0..6acaffbf 100644 --- a/pkg/lexer/liquid.go +++ b/pkg/lexer/liquid.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Liquid lexer. -type Liquid struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLiquid.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Liquid) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"liquid"}, Filenames: []string{"*.liquid"}, }, @@ -22,10 +29,5 @@ func (l Liquid) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Liquid) Name() string { - return heartbeat.LanguageLiquid.StringChroma() + )) } diff --git a/pkg/lexer/literateagda.go b/pkg/lexer/literateagda.go index ac2df4dc..5e1ae044 100644 --- a/pkg/lexer/literateagda.go +++ b/pkg/lexer/literateagda.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// LiterateAgda lexer. -type LiterateAgda struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLiterateAgda.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l LiterateAgda) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"lagda", "literate-agda"}, Filenames: []string{"*.lagda"}, MimeTypes: []string{"text/x-literate-agda"}, @@ -23,10 +30,5 @@ func (l LiterateAgda) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (LiterateAgda) Name() string { - return heartbeat.LanguageLiterateAgda.StringChroma() + )) } diff --git a/pkg/lexer/literatecryptol.go b/pkg/lexer/literatecryptol.go index 0843345f..0ad322d4 100644 --- a/pkg/lexer/literatecryptol.go +++ b/pkg/lexer/literatecryptol.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// LiterateCryptol lexer. -type LiterateCryptol struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLiterateCryptol.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l LiterateCryptol) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"lcry", "literate-cryptol", "lcryptol"}, Filenames: []string{"*.lcry"}, MimeTypes: []string{"text/x-literate-cryptol"}, @@ -23,10 +30,5 @@ func (l LiterateCryptol) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (LiterateCryptol) Name() string { - return heartbeat.LanguageLiterateCryptol.StringChroma() + )) } diff --git a/pkg/lexer/literatehaskell.go b/pkg/lexer/literatehaskell.go index ff86a181..38ee8884 100644 --- a/pkg/lexer/literatehaskell.go +++ b/pkg/lexer/literatehaskell.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// LiterateHaskell lexer. -type LiterateHaskell struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLiterateHaskell.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l LiterateHaskell) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"lhs", "literate-haskell", "lhaskell"}, Filenames: []string{"*.lhs"}, MimeTypes: []string{"text/x-literate-haskell"}, @@ -23,10 +30,5 @@ func (l LiterateHaskell) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (LiterateHaskell) Name() string { - return heartbeat.LanguageLiterateHaskell.StringChroma() + )) } diff --git a/pkg/lexer/literateidris.go b/pkg/lexer/literateidris.go index 58c617f2..88823355 100644 --- a/pkg/lexer/literateidris.go +++ b/pkg/lexer/literateidris.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// LiterateIdris lexer. -type LiterateIdris struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLiterateIdris.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l LiterateIdris) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"lidr", "literate-idris", "lidris"}, Filenames: []string{"*.lidr"}, MimeTypes: []string{"text/x-literate-idris"}, @@ -23,10 +30,5 @@ func (l LiterateIdris) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (LiterateIdris) Name() string { - return heartbeat.LanguageLiterateIdris.StringChroma() + )) } diff --git a/pkg/lexer/livescript.go b/pkg/lexer/livescript.go index 41480c7a..f8435b64 100644 --- a/pkg/lexer/livescript.go +++ b/pkg/lexer/livescript.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// LiveScript lexer. -type LiveScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLiveScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l LiveScript) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"live-script", "livescript"}, Filenames: []string{"*.ls"}, MimeTypes: []string{"text/livescript"}, @@ -23,10 +30,5 @@ func (l LiveScript) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (LiveScript) Name() string { - return heartbeat.LanguageLiveScript.StringChroma() + )) } diff --git a/pkg/lexer/llvmmir.go b/pkg/lexer/llvmmir.go index 493ec079..42386111 100644 --- a/pkg/lexer/llvmmir.go +++ b/pkg/lexer/llvmmir.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// LLVMMIR lexer. -type LLVMMIR struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLLVMMIR.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l LLVMMIR) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"llvm-mir"}, Filenames: []string{"*.mir"}, }, @@ -22,10 +29,5 @@ func (l LLVMMIR) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (LLVMMIR) Name() string { - return heartbeat.LanguageLLVMMIR.StringChroma() + )) } diff --git a/pkg/lexer/llvmmirbody.go b/pkg/lexer/llvmmirbody.go index 9ad3a7a6..fd107154 100644 --- a/pkg/lexer/llvmmirbody.go +++ b/pkg/lexer/llvmmirbody.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// LLVMMIRBODY lexer. -type LLVMMIRBODY struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLLVMMIRBody.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l LLVMMIRBODY) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"llvm-mir-body"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l LLVMMIRBODY) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (LLVMMIRBODY) Name() string { - return heartbeat.LanguageLLVMMIRBody.StringChroma() + )) } diff --git a/pkg/lexer/logos.go b/pkg/lexer/logos.go index 2f66a288..78ba0aa5 100644 --- a/pkg/lexer/logos.go +++ b/pkg/lexer/logos.go @@ -4,20 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var logosAnalyserKeywordsRe = regexp.MustCompile(`%(?:hook|ctor|init|c\()`) -// Logos lexer. -type Logos struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLogos.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Logos) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"logos"}, Filenames: []string{"*.x", "*.xi", "*.xm", "*.xmi"}, MimeTypes: []string{"text/x-logos"}, @@ -28,20 +35,11 @@ func (l Logos) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if logosAnalyserKeywordsRe.MatchString(text) { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Logos) Name() string { - return heartbeat.LanguageLogos.StringChroma() + })) } diff --git a/pkg/lexer/logos_test.go b/pkg/lexer/logos_test.go old mode 100644 new mode 100755 index 5e940a6a..2acdbd56 --- a/pkg/lexer/logos_test.go +++ b/pkg/lexer/logos_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestLogos_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/logos_basic.xm") assert.NoError(t, err) - l := lexer.Logos{}.Lexer() + l := lexers.Get(heartbeat.LanguageLogos.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/logtalk.go b/pkg/lexer/logtalk.go index 0175de02..c913e8c3 100644 --- a/pkg/lexer/logtalk.go +++ b/pkg/lexer/logtalk.go @@ -5,20 +5,27 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var logtalkAnalyserSyntaxRe = regexp.MustCompile(`(?m)^:-\s[a-z]`) -// Logtalk lexer. -type Logtalk struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLogtalk.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Logtalk) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"logtalk"}, Filenames: []string{"*.lgt", "*.logtalk"}, MimeTypes: []string{"text/x-logtalk"}, @@ -28,9 +35,7 @@ func (l Logtalk) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if strings.Contains(text, ":- object(") || strings.Contains(text, ":- protocol(") || strings.Contains(text, ":- category(") { @@ -42,12 +47,5 @@ func (l Logtalk) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Logtalk) Name() string { - return heartbeat.LanguageLogtalk.StringChroma() + })) } diff --git a/pkg/lexer/logtalk_test.go b/pkg/lexer/logtalk_test.go old mode 100644 new mode 100755 index 90a6bc0f..a5f89774 --- a/pkg/lexer/logtalk_test.go +++ b/pkg/lexer/logtalk_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestLogtalk_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestLogtalk_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Logtalk{}.Lexer() + l := lexers.Get(heartbeat.LanguageLogtalk.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/lsl.go b/pkg/lexer/lsl.go index 9d516f35..58b6fe49 100644 --- a/pkg/lexer/lsl.go +++ b/pkg/lexer/lsl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// LSL lexer. -type LSL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageLSL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l LSL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"lsl"}, Filenames: []string{"*.lsl"}, MimeTypes: []string{"text/x-lsl"}, @@ -23,10 +30,5 @@ func (l LSL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (LSL) Name() string { - return heartbeat.LanguageLSL.StringChroma() + )) } diff --git a/pkg/lexer/make.go b/pkg/lexer/make.go index 447f16ca..122d8a32 100644 --- a/pkg/lexer/make.go +++ b/pkg/lexer/make.go @@ -4,40 +4,29 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) var makefileAnalyserVariableRe = regexp.MustCompile(`\$\([A-Z_]+\)`) -// Makefile lexer. -type Makefile struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMakefile.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Makefile) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - // Many makefiles have $(BIG_CAPS) style variables. - if makefileAnalyserVariableRe.MatchString(text) { - return 0.1 - } + lexer.SetAnalyser(func(text string) float32 { + // Many makefiles have $(BIG_CAPS) style variables. + if makefileAnalyserVariableRe.MatchString(text) { + return 0.1 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Makefile) Name() string { - return heartbeat.LanguageMakefile.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/make_test.go b/pkg/lexer/make_test.go old mode 100644 new mode 100755 index 9153c2aa..0ed79392 --- a/pkg/lexer/make_test.go +++ b/pkg/lexer/make_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestMakefile_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/makefile") assert.NoError(t, err) - l := lexer.Makefile{}.Lexer() + l := lexers.Get(heartbeat.LanguageMakefile.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.1), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/maql.go b/pkg/lexer/maql.go index 8ebebc45..67491c11 100644 --- a/pkg/lexer/maql.go +++ b/pkg/lexer/maql.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MAQL lexer. -type MAQL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMAQL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MAQL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"maql"}, Filenames: []string{"*.maql"}, MimeTypes: []string{"text/x-gooddata-maql", "application/x-gooddata-maql"}, @@ -23,10 +30,5 @@ func (l MAQL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MAQL) Name() string { - return heartbeat.LanguageMAQL.StringChroma() + )) } diff --git a/pkg/lexer/marko.go b/pkg/lexer/marko.go index 1261294e..d8994759 100644 --- a/pkg/lexer/marko.go +++ b/pkg/lexer/marko.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Marko lexer. -type Marko struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMarko.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Marko) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"marko"}, Filenames: []string{"*.marko"}, MimeTypes: []string{"text/x-marko"}, @@ -23,10 +30,5 @@ func (l Marko) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Marko) Name() string { - return heartbeat.LanguageMarko.StringChroma() + )) } diff --git a/pkg/lexer/mask.go b/pkg/lexer/mask.go index 295b70f4..82b30678 100644 --- a/pkg/lexer/mask.go +++ b/pkg/lexer/mask.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Mask lexer. -type Mask struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMask.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Mask) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mask"}, Filenames: []string{"*.mask"}, MimeTypes: []string{"text/x-mask"}, @@ -23,10 +30,5 @@ func (l Mask) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Mask) Name() string { - return heartbeat.LanguageMask.StringChroma() + )) } diff --git a/pkg/lexer/matlab.go b/pkg/lexer/matlab.go index 5f96fc0f..fad86214 100644 --- a/pkg/lexer/matlab.go +++ b/pkg/lexer/matlab.go @@ -5,8 +5,8 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) @@ -15,57 +15,46 @@ var ( matlabAnalyserSystemCMDRe = regexp.MustCompile(`^!\w+`) ) -// Matlab lexer. -type Matlab struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMatlab.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Matlab) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - lines := strings.Split(strings.ReplaceAll(text, "\r\n", "\n"), "\n") + lexer.SetAnalyser(func(text string) float32 { + lines := strings.Split(strings.ReplaceAll(text, "\r\n", "\n"), "\n") - var firstNonComment string - for _, line := range lines { - if !matlabAnalyserCommentRe.MatchString(line) { - firstNonComment = strings.TrimSpace(line) - break - } + var firstNonComment string + for _, line := range lines { + if !matlabAnalyserCommentRe.MatchString(line) { + firstNonComment = strings.TrimSpace(line) + break } + } - // function declaration - if strings.HasPrefix(firstNonComment, "function") && !strings.Contains(firstNonComment, "{") { - return 1.0 - } + // function declaration + if strings.HasPrefix(firstNonComment, "function") && !strings.Contains(firstNonComment, "{") { + return 1.0 + } - // comment - for _, line := range lines { - if matlabAnalyserCommentRe.MatchString(line) { - return 0.2 - } + // comment + for _, line := range lines { + if matlabAnalyserCommentRe.MatchString(line) { + return 0.2 } + } - // system cmd - for _, line := range lines { - if matlabAnalyserSystemCMDRe.MatchString(line) { - return 0.2 - } + // system cmd + for _, line := range lines { + if matlabAnalyserSystemCMDRe.MatchString(line) { + return 0.2 } + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Matlab) Name() string { - return heartbeat.LanguageMatlab.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/matlab_test.go b/pkg/lexer/matlab_test.go old mode 100644 new mode 100755 index 440a10ed..da29361a --- a/pkg/lexer/matlab_test.go +++ b/pkg/lexer/matlab_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestMatlab_AnalyseText(t *testing.T) { @@ -37,7 +39,8 @@ func TestMatlab_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Matlab{}.Lexer() + l := lexers.Get(heartbeat.LanguageMatlab.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/matlabsession.go b/pkg/lexer/matlabsession.go index 01ed80c7..32a015b6 100644 --- a/pkg/lexer/matlabsession.go +++ b/pkg/lexer/matlabsession.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MatlabSession lexer. -type MatlabSession struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMatlabSession.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MatlabSession) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"matlabsession"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l MatlabSession) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MatlabSession) Name() string { - return heartbeat.LanguageMatlabSession.StringChroma() + )) } diff --git a/pkg/lexer/mime.go b/pkg/lexer/mime.go index 8af6d9be..0d6809f2 100644 --- a/pkg/lexer/mime.go +++ b/pkg/lexer/mime.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MIME lexer. -type MIME struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMIME.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MIME) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"mime"}, + Name: language, + Aliases: []string{"mime"}, + MimeTypes: []string{"multipart/mixed", "multipart/related", "multipart/alternative"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l MIME) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MIME) Name() string { - return heartbeat.LanguageMIME.StringChroma() + )) } diff --git a/pkg/lexer/minid.go b/pkg/lexer/minid.go index 587cd25e..eb4d1c93 100644 --- a/pkg/lexer/minid.go +++ b/pkg/lexer/minid.go @@ -2,20 +2,26 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MiniD lexer. -type MiniD struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMiniD.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MiniD) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"minid"}, - // Don't lex .md as MiniD, reserve for Markdown. + Name: language, + Aliases: []string{"minid"}, Filenames: []string{}, MimeTypes: []string{"text/x-minidsrc"}, }, @@ -24,10 +30,5 @@ func (l MiniD) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MiniD) Name() string { - return heartbeat.LanguageMiniD.StringChroma() + )) } diff --git a/pkg/lexer/miniscript.go b/pkg/lexer/miniscript.go index 72d6cf66..91c2fe57 100644 --- a/pkg/lexer/miniscript.go +++ b/pkg/lexer/miniscript.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MiniScript lexer. -type MiniScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMiniScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MiniScript) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ms", "miniscript"}, Filenames: []string{"*.ms"}, MimeTypes: []string{"text/x-miniscript", "application/x-miniscript"}, @@ -23,10 +30,5 @@ func (l MiniScript) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MiniScript) Name() string { - return heartbeat.LanguageMiniScript.StringChroma() + )) } diff --git a/pkg/lexer/modelica.go b/pkg/lexer/modelica.go index 9be62124..ca9f37d0 100644 --- a/pkg/lexer/modelica.go +++ b/pkg/lexer/modelica.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Modelica lexer. -type Modelica struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageModelica.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Modelica) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"modelica"}, Filenames: []string{"*.mo"}, MimeTypes: []string{"text/x-modelica"}, @@ -23,10 +30,5 @@ func (l Modelica) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Modelica) Name() string { - return heartbeat.LanguageModelica.StringChroma() + )) } diff --git a/pkg/lexer/modula2.go b/pkg/lexer/modula2.go index 6a2dc589..d4921040 100644 --- a/pkg/lexer/modula2.go +++ b/pkg/lexer/modula2.go @@ -5,8 +5,8 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) @@ -15,48 +15,37 @@ var ( modula2AnalyserFunctionRe = regexp.MustCompile(`\bFUNCTION\b`) ) -// Modula2 lexer. -type Modula2 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageModula2.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Modula2) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - // It's Pascal-like, but does not use FUNCTION -- uses PROCEDURE - // instead. + lexer.SetAnalyser(func(text string) float32 { + // It's Pascal-like, but does not use FUNCTION -- uses PROCEDURE + // instead. - // Check if this looks like Pascal, if not, bail out early - if !strings.Contains(text, "(*") && !strings.Contains(text, "*)") && !strings.Contains(text, ":=") { - return 0 - } + // Check if this looks like Pascal, if not, bail out early + if !strings.Contains(text, "(*") && !strings.Contains(text, "*)") && !strings.Contains(text, ":=") { + return 0 + } - var result float32 + var result float32 - // Procedure is in Modula2 - if modula2AnalyserProcedureRe.MatchString(text) { - result += 0.6 - } + // Procedure is in Modula2 + if modula2AnalyserProcedureRe.MatchString(text) { + result += 0.6 + } - // FUNCTION is only valid in Pascal, but not in Modula2 - if modula2AnalyserFunctionRe.MatchString(text) { - result = 0 - } + // FUNCTION is only valid in Pascal, but not in Modula2 + if modula2AnalyserFunctionRe.MatchString(text) { + result = 0 + } - return result - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Modula2) Name() string { - return heartbeat.LanguageModula2.StringChroma() + return result + }) } diff --git a/pkg/lexer/modula2_test.go b/pkg/lexer/modula2_test.go old mode 100644 new mode 100755 index 4ee34805..0c56bd7b --- a/pkg/lexer/modula2_test.go +++ b/pkg/lexer/modula2_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestModula2_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestModula2_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Modula2{}.Lexer() + l := lexers.Get(heartbeat.LanguageModula2.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/mojo.go b/pkg/lexer/mojo.go index 27f9d344..a237f6c2 100644 --- a/pkg/lexer/mojo.go +++ b/pkg/lexer/mojo.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Mojo lexer. -type Mojo struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMojo.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Mojo) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mojo"}, Filenames: []string{"*.🔥", "*.mojo"}, MimeTypes: []string{"text/x-mojo"}, @@ -23,10 +30,5 @@ func (l Mojo) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Mojo) Name() string { - return heartbeat.LanguageMojo.StringChroma() + )) } diff --git a/pkg/lexer/monkey.go b/pkg/lexer/monkey.go index b99bd11a..70af9776 100644 --- a/pkg/lexer/monkey.go +++ b/pkg/lexer/monkey.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Monkey lexer. -type Monkey struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMonkey.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Monkey) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"monkey"}, Filenames: []string{"*.monkey"}, MimeTypes: []string{"text/x-monkey"}, @@ -23,10 +30,5 @@ func (l Monkey) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Monkey) Name() string { - return heartbeat.LanguageMonkey.StringChroma() + )) } diff --git a/pkg/lexer/monte.go b/pkg/lexer/monte.go index b821db2f..52498942 100644 --- a/pkg/lexer/monte.go +++ b/pkg/lexer/monte.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Monte lexer. -type Monte struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMonte.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Monte) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"monte"}, Filenames: []string{"*.mt"}, }, @@ -22,10 +29,5 @@ func (l Monte) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Monte) Name() string { - return heartbeat.LanguageMonte.StringChroma() + )) } diff --git a/pkg/lexer/moocode.go b/pkg/lexer/moocode.go index 81777381..efda479d 100644 --- a/pkg/lexer/moocode.go +++ b/pkg/lexer/moocode.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MOOCode lexer. -type MOOCode struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMOOCode.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MOOCode) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"moocode", "moo"}, Filenames: []string{"*.moo"}, MimeTypes: []string{"text/x-moocode"}, @@ -23,10 +30,5 @@ func (l MOOCode) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MOOCode) Name() string { - return heartbeat.LanguageMOOCode.StringChroma() + )) } diff --git a/pkg/lexer/moonscript.go b/pkg/lexer/moonscript.go index 1f812743..2d9f6eb8 100644 --- a/pkg/lexer/moonscript.go +++ b/pkg/lexer/moonscript.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MoonScript lexer. -type MoonScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMoonScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MoonScript) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"moon", "moonscript"}, + Name: language, + Aliases: []string{"moon", "moonscript"}, + MimeTypes: []string{"text/x-moonscript", "application/x-moonscript"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l MoonScript) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MoonScript) Name() string { - return heartbeat.LanguageMoonScript.StringChroma() + )) } diff --git a/pkg/lexer/mosel.go b/pkg/lexer/mosel.go index bea6589f..5b09ad52 100644 --- a/pkg/lexer/mosel.go +++ b/pkg/lexer/mosel.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Mosel lexer. -type Mosel struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMosel.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Mosel) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"model"}, Filenames: []string{"*.mos"}, }, @@ -22,10 +29,5 @@ func (l Mosel) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Mosel) Name() string { - return heartbeat.LanguageMosel.StringChroma() + )) } diff --git a/pkg/lexer/mozhashpreproc.go b/pkg/lexer/mozhashpreproc.go index 8a88e64f..e5e4abc4 100644 --- a/pkg/lexer/mozhashpreproc.go +++ b/pkg/lexer/mozhashpreproc.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MozPreprocHash lexer. -type MozPreprocHash struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMozPreprocHash.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MozPreprocHash) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mozhashpreproc"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l MozPreprocHash) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MozPreprocHash) Name() string { - return heartbeat.LanguageMozPreprocHash.StringChroma() + )) } diff --git a/pkg/lexer/mozpreprocpercent.go b/pkg/lexer/mozpreprocpercent.go index ca9057b5..1c3f5c3f 100644 --- a/pkg/lexer/mozpreprocpercent.go +++ b/pkg/lexer/mozpreprocpercent.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MozPreprocPercent lexer. -type MozPreprocPercent struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMozPreprocPercent.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MozPreprocPercent) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mozpercentpreproc"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l MozPreprocPercent) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MozPreprocPercent) Name() string { - return heartbeat.LanguageMozPreprocPercent.StringChroma() + )) } diff --git a/pkg/lexer/mql.go b/pkg/lexer/mql.go index 02d73530..28393c10 100644 --- a/pkg/lexer/mql.go +++ b/pkg/lexer/mql.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MQL lexer. -type MQL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMQL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MQL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mql", "mq4", "mq5", "mql4", "mql5"}, Filenames: []string{"*.mq4", "*.mq5", "*.mqh"}, MimeTypes: []string{"text/x-mql"}, @@ -23,10 +30,5 @@ func (l MQL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MQL) Name() string { - return heartbeat.LanguageMQL.StringChroma() + )) } diff --git a/pkg/lexer/mscgen.go b/pkg/lexer/mscgen.go index c47c3885..872c4f9b 100644 --- a/pkg/lexer/mscgen.go +++ b/pkg/lexer/mscgen.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Mscgen lexer. -type Mscgen struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMscgen.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Mscgen) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mscgen", "msc"}, Filenames: []string{"*.msc"}, }, @@ -22,10 +29,5 @@ func (l Mscgen) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Mscgen) Name() string { - return heartbeat.LanguageMscgen.StringChroma() + )) } diff --git a/pkg/lexer/msdossession.go b/pkg/lexer/msdossession.go index 800ab77f..d2491be4 100644 --- a/pkg/lexer/msdossession.go +++ b/pkg/lexer/msdossession.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MSDOSSession lexer. -type MSDOSSession struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMSDOSSession.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MSDOSSession) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"doscon"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l MSDOSSession) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MSDOSSession) Name() string { - return heartbeat.LanguageMSDOSSession.StringChroma() + )) } diff --git a/pkg/lexer/mupad.go b/pkg/lexer/mupad.go index 26e87e3b..279ff71a 100644 --- a/pkg/lexer/mupad.go +++ b/pkg/lexer/mupad.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MuPAD lexer. -type MuPAD struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMuPAD.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MuPAD) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mupad"}, Filenames: []string{"*.mu"}, }, @@ -22,10 +29,5 @@ func (l MuPAD) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MuPAD) Name() string { - return heartbeat.LanguageMuPAD.StringChroma() + )) } diff --git a/pkg/lexer/mustache.go b/pkg/lexer/mustache.go index 2a2e6d00..76d931ff 100644 --- a/pkg/lexer/mustache.go +++ b/pkg/lexer/mustache.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Mustache lexer. -type Mustache struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMustache.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Mustache) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mustache"}, Filenames: []string{"*.mustache"}, MimeTypes: []string{"text/x-mustache-template"}, @@ -23,10 +30,5 @@ func (l Mustache) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Mustache) Name() string { - return heartbeat.LanguageMustache.StringChroma() + )) } diff --git a/pkg/lexer/mxml.go b/pkg/lexer/mxml.go index 8433b555..222b022a 100644 --- a/pkg/lexer/mxml.go +++ b/pkg/lexer/mxml.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// MXML lexer. -type MXML struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageMXML.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l MXML) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"mxml"}, Filenames: []string{"*.mxml"}, MimeTypes: []string{"text/xml", "application/xml"}, @@ -23,10 +30,5 @@ func (l MXML) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (MXML) Name() string { - return heartbeat.LanguageMXML.StringChroma() + )) } diff --git a/pkg/lexer/nasm.go b/pkg/lexer/nasm.go index e0b7f45e..9aeeb2cb 100644 --- a/pkg/lexer/nasm.go +++ b/pkg/lexer/nasm.go @@ -3,41 +3,30 @@ package lexer import ( "regexp" - "github.com/alecthomas/chroma/v2" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2/lexers" ) var nasmAnalyzerRe = regexp.MustCompile(`(?i)PROC`) -// NASM lexer. -type NASM struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNASM.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l NASM) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - // Probably TASM - if nasmAnalyzerRe.MatchString(text) { - return 0 - } - + lexer.SetAnalyser(func(text string) float32 { + // Probably TASM + if nasmAnalyzerRe.MatchString(text) { return 0 - }) - - return lexer - } - - return nil -} + } -// Name returns the name of the lexer. -func (NASM) Name() string { - return heartbeat.LanguageNASM.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/nasm_test.go b/pkg/lexer/nasm_test.go old mode 100644 new mode 100755 index 71407b01..ae3bcf7c --- a/pkg/lexer/nasm_test.go +++ b/pkg/lexer/nasm_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNASM_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/nasm.asm") assert.NoError(t, err) - l := lexer.NASM{}.Lexer() + l := lexers.Get(heartbeat.LanguageNASM.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/nasmobjdump.go b/pkg/lexer/nasmobjdump.go index b4aebeab..d48e9063 100644 --- a/pkg/lexer/nasmobjdump.go +++ b/pkg/lexer/nasmobjdump.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// NASMObjdump lexer. -type NASMObjdump struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNASMObjdump.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l NASMObjdump) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"objdump-nasm"}, Filenames: []string{"*.objdump-intel"}, MimeTypes: []string{"text/x-nasm-objdump"}, @@ -23,10 +30,5 @@ func (l NASMObjdump) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (NASMObjdump) Name() string { - return heartbeat.LanguageNASMObjdump.StringChroma() + )) } diff --git a/pkg/lexer/ncl.go b/pkg/lexer/ncl.go index cc3b83f6..f3268d6b 100644 --- a/pkg/lexer/ncl.go +++ b/pkg/lexer/ncl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// NCL lexer. -type NCL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNCL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l NCL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ncl"}, Filenames: []string{"*.ncl"}, MimeTypes: []string{"text/ncl"}, @@ -23,10 +30,5 @@ func (l NCL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (NCL) Name() string { - return heartbeat.LanguageNCL.StringChroma() + )) } diff --git a/pkg/lexer/nemerle.go b/pkg/lexer/nemerle.go index 6fd5dcc2..ea5c6b23 100644 --- a/pkg/lexer/nemerle.go +++ b/pkg/lexer/nemerle.go @@ -4,21 +4,27 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Nemerle lexer. -type Nemerle struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNemerle.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Nemerle) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"nemerle"}, Filenames: []string{"*.n"}, - // inferred MimeTypes: []string{"text/x-nemerle"}, }, func() chroma.Rules { @@ -26,9 +32,7 @@ func (l Nemerle) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // Nemerle is quite similar to Python, but @if is relatively uncommon // elsewhere. if strings.Contains(text, "@if") { @@ -36,12 +40,5 @@ func (l Nemerle) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Nemerle) Name() string { - return heartbeat.LanguageNemerle.StringChroma() + })) } diff --git a/pkg/lexer/nemerle_test.go b/pkg/lexer/nemerle_test.go old mode 100644 new mode 100755 index b14c048b..97879215 --- a/pkg/lexer/nemerle_test.go +++ b/pkg/lexer/nemerle_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNermerle_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/nemerle_if.n") assert.NoError(t, err) - l := lexer.Nemerle{}.Lexer() + l := lexers.Get(heartbeat.LanguageNemerle.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.1), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/nesc.go b/pkg/lexer/nesc.go index 1c1b1b17..f35d72ed 100644 --- a/pkg/lexer/nesc.go +++ b/pkg/lexer/nesc.go @@ -2,19 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// NesC lexer. -type NesC struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNesC.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l NesC) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"nesc"}, Filenames: []string{"*.nc"}, MimeTypes: []string{"text/x-nescsrc"}, @@ -24,21 +30,12 @@ func (l NesC) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { c := lexers.Get(heartbeat.LanguageC.StringChroma()) if c == nil { return 0 } return c.AnalyseText(text) - }) - - return lexer -} - -// Name returns the name of the lexer. -func (NesC) Name() string { - return heartbeat.LanguageNesC.StringChroma() + })) } diff --git a/pkg/lexer/nesc_test.go b/pkg/lexer/nesc_test.go old mode 100644 new mode 100755 index 840c3f0e..05ad0245 --- a/pkg/lexer/nesc_test.go +++ b/pkg/lexer/nesc_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNesc_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestNesc_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.NesC{}.Lexer() + l := lexers.Get(heartbeat.LanguageNesC.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/newlisp.go b/pkg/lexer/newlisp.go index c941220f..9400f0c3 100644 --- a/pkg/lexer/newlisp.go +++ b/pkg/lexer/newlisp.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// NewLisp lexer. -type NewLisp struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNewLisp.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l NewLisp) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"newlisp"}, Filenames: []string{"*.lsp", "*.nl", "*.kif"}, MimeTypes: []string{"text/x-newlisp", "application/x-newlisp"}, @@ -23,10 +30,5 @@ func (l NewLisp) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (NewLisp) Name() string { - return heartbeat.LanguageNewLisp.StringChroma() + )) } diff --git a/pkg/lexer/nit.go b/pkg/lexer/nit.go index 66d6cd15..8760a61a 100644 --- a/pkg/lexer/nit.go +++ b/pkg/lexer/nit.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Nit lexer. -type Nit struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNit.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Nit) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"nit"}, Filenames: []string{"*.nit"}, }, @@ -22,10 +29,5 @@ func (l Nit) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Nit) Name() string { - return heartbeat.LanguageNit.StringChroma() + )) } diff --git a/pkg/lexer/notmuch.go b/pkg/lexer/notmuch.go index 2738a178..49071b4c 100644 --- a/pkg/lexer/notmuch.go +++ b/pkg/lexer/notmuch.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Notmuch lexer. -type Notmuch struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNotmuch.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Notmuch) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"notmuch"}, }, func() chroma.Rules { @@ -23,20 +30,11 @@ func (l Notmuch) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if strings.HasPrefix(text, "\fmessage{") { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Notmuch) Name() string { - return heartbeat.LanguageNotmuch.StringChroma() + })) } diff --git a/pkg/lexer/notmuch_test.go b/pkg/lexer/notmuch_test.go old mode 100644 new mode 100755 index 1808696c..273cb4dd --- a/pkg/lexer/notmuch_test.go +++ b/pkg/lexer/notmuch_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNotmuch_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/notmuch") assert.NoError(t, err) - l := lexer.Notmuch{}.Lexer() + l := lexers.Get(heartbeat.LanguageNotmuch.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/nsis.go b/pkg/lexer/nsis.go index 4104d0dc..add930df 100644 --- a/pkg/lexer/nsis.go +++ b/pkg/lexer/nsis.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// NSIS lexer. -type NSIS struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNSIS.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l NSIS) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"nsis", "nsi", "nsh"}, Filenames: []string{"*.nsi", "*.nsh"}, MimeTypes: []string{"text/x-nsis"}, @@ -23,10 +30,5 @@ func (l NSIS) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (NSIS) Name() string { - return heartbeat.LanguageNSIS.StringChroma() + )) } diff --git a/pkg/lexer/numpy.go b/pkg/lexer/numpy.go index 69d22ed0..5b4e9825 100644 --- a/pkg/lexer/numpy.go +++ b/pkg/lexer/numpy.go @@ -4,19 +4,26 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/shebang" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// NumPy lexer. -type NumPy struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNumPy.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l NumPy) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"numpy"}, }, func() chroma.Rules { @@ -24,9 +31,7 @@ func (l NumPy) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { hasPythonShebang, _ := shebang.MatchString(text, `pythonw?(3(\.\d)?)?`) containsNumpyImport := strings.Contains(text, "import numpy") containsFromNumpyImport := strings.Contains(text, "from numpy import") @@ -44,12 +49,5 @@ func (l NumPy) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (NumPy) Name() string { - return heartbeat.LanguageNumPy.StringChroma() + })) } diff --git a/pkg/lexer/numpy_test.go b/pkg/lexer/numpy_test.go old mode 100644 new mode 100755 index 6fcb82c7..3f18b8fa --- a/pkg/lexer/numpy_test.go +++ b/pkg/lexer/numpy_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNumPy_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestNumPy_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.NumPy{}.Lexer() + l := lexers.Get(heartbeat.LanguageNumPy.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/nushell.go b/pkg/lexer/nushell.go index 90cdb323..c1b82713 100644 --- a/pkg/lexer/nushell.go +++ b/pkg/lexer/nushell.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Nushell lexer. -type Nushell struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNushell.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Nushell) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"Nu"}, Filenames: []string{"*.nu"}, }, @@ -22,10 +29,5 @@ func (l Nushell) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Nushell) Name() string { - return heartbeat.LanguageNushell.StringChroma() + )) } diff --git a/pkg/lexer/nusmv.go b/pkg/lexer/nusmv.go index b48125f0..15cad14c 100644 --- a/pkg/lexer/nusmv.go +++ b/pkg/lexer/nusmv.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// NuSMV lexer. -type NuSMV struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageNuSMV.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l NuSMV) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"nusmv"}, Filenames: []string{"*.smv"}, }, @@ -22,10 +29,5 @@ func (l NuSMV) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (NuSMV) Name() string { - return heartbeat.LanguageNuSMV.StringChroma() + )) } diff --git a/pkg/lexer/objdump.go b/pkg/lexer/objdump.go index dda8345f..bf6ab402 100644 --- a/pkg/lexer/objdump.go +++ b/pkg/lexer/objdump.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Objdump lexer. -type Objdump struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageObjdump.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Objdump) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"objdump"}, Filenames: []string{"*.objdump"}, MimeTypes: []string{"text/x-objdump"}, @@ -23,10 +30,5 @@ func (l Objdump) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Objdump) Name() string { - return heartbeat.LanguageObjdump.StringChroma() + )) } diff --git a/pkg/lexer/objectivec.go b/pkg/lexer/objectivec.go index 1176c783..88649ef7 100644 --- a/pkg/lexer/objectivec.go +++ b/pkg/lexer/objectivec.go @@ -5,8 +5,8 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) @@ -23,44 +23,33 @@ var ( objectiveCAnalyserNSNumberRe = regexp.MustCompile(`@[0-9]+`) ) -// ObjectiveC lexer. -type ObjectiveC struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageObjectiveC.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ObjectiveC) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if objectiveCAnalyserKeywordsRe.MatchString(text) { - return 1.0 - } + lexer.SetAnalyser(func(text string) float32 { + if objectiveCAnalyserKeywordsRe.MatchString(text) { + return 1.0 + } - if strings.Contains(text, `@"`) { - return 0.8 - } + if strings.Contains(text, `@"`) { + return 0.8 + } - if objectiveCAnalyserNSNumberRe.MatchString(text) { - return 0.7 - } + if objectiveCAnalyserNSNumberRe.MatchString(text) { + return 0.7 + } - if objectiveCAnalyserMessageRe.MatchString(text) { - return 0.8 - } + if objectiveCAnalyserMessageRe.MatchString(text) { + return 0.8 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (ObjectiveC) Name() string { - return heartbeat.LanguageObjectiveC.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/objectivec_test.go b/pkg/lexer/objectivec_test.go old mode 100644 new mode 100755 index dbcb5842..353feb4d --- a/pkg/lexer/objectivec_test.go +++ b/pkg/lexer/objectivec_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestObjectiveC_AnalyseText(t *testing.T) { @@ -45,7 +47,8 @@ func TestObjectiveC_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.ObjectiveC{}.Lexer() + l := lexers.Get(heartbeat.LanguageObjectiveC.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/objectivecpp.go b/pkg/lexer/objectivecpp.go index 1be95400..2ea0e39a 100644 --- a/pkg/lexer/objectivecpp.go +++ b/pkg/lexer/objectivecpp.go @@ -2,22 +2,29 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ObjectiveCPP lexer. -type ObjectiveCPP struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageObjectiveCPP.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ObjectiveCPP) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"objective-c++", "objectivec++", "obj-c++", "objc++"}, Filenames: []string{"*.mm", "*.hh"}, MimeTypes: []string{"text/x-objective-c++"}, - // Lower than C++. + Priority: 0.05, }, func() chroma.Rules { @@ -25,10 +32,5 @@ func (l ObjectiveCPP) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ObjectiveCPP) Name() string { - return heartbeat.LanguageObjectiveCPP.StringChroma() + )) } diff --git a/pkg/lexer/objectivej.go b/pkg/lexer/objectivej.go index 34033ccf..68e76cbd 100644 --- a/pkg/lexer/objectivej.go +++ b/pkg/lexer/objectivej.go @@ -4,20 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var objectiveJAnalyserImportRe = regexp.MustCompile(`(?m)^\s*@import\s+[<"]`) -// ObjectiveJ lexer. -type ObjectiveJ struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageObjectiveJ.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ObjectiveJ) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"objective-j", "objectivej", "obj-j", "objj"}, Filenames: []string{"*.j"}, MimeTypes: []string{"text/x-objective-j"}, @@ -27,21 +34,12 @@ func (l ObjectiveJ) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // special directive found in most Objective-J files. if objectiveJAnalyserImportRe.MatchString(text) { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (ObjectiveJ) Name() string { - return heartbeat.LanguageObjectiveJ.StringChroma() + })) } diff --git a/pkg/lexer/objectivej_test.go b/pkg/lexer/objectivej_test.go old mode 100644 new mode 100755 index 0f9920f8..04294561 --- a/pkg/lexer/objectivej_test.go +++ b/pkg/lexer/objectivej_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestObjectiveJ_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/objectivej_import.j") assert.NoError(t, err) - l := lexer.ObjectiveJ{}.Lexer() + l := lexers.Get(heartbeat.LanguageObjectiveJ.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/ooc.go b/pkg/lexer/ooc.go index 9ae3f9ed..04049a6d 100644 --- a/pkg/lexer/ooc.go +++ b/pkg/lexer/ooc.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Ooc lexer. -type Ooc struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageOoc.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Ooc) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ooc"}, Filenames: []string{"*.ooc"}, MimeTypes: []string{"text/x-ooc"}, @@ -23,10 +30,5 @@ func (l Ooc) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Ooc) Name() string { - return heartbeat.LanguageOoc.StringChroma() + )) } diff --git a/pkg/lexer/opa.go b/pkg/lexer/opa.go index ce1eb10d..a6db3d15 100644 --- a/pkg/lexer/opa.go +++ b/pkg/lexer/opa.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Opa lexer. -type Opa struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageOpa.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Opa) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"opa"}, Filenames: []string{"*.opa"}, MimeTypes: []string{"text/x-opa"}, @@ -23,10 +30,5 @@ func (l Opa) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Opa) Name() string { - return heartbeat.LanguageOpa.StringChroma() + )) } diff --git a/pkg/lexer/openedgeabl.go b/pkg/lexer/openedgeabl.go index 04fb5f1e..6d065113 100644 --- a/pkg/lexer/openedgeabl.go +++ b/pkg/lexer/openedgeabl.go @@ -4,48 +4,37 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// OpenEdgeABL lexer. -type OpenEdgeABL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageOpenEdgeABL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l OpenEdgeABL) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - // try to identify OpenEdge ABL based on a few common constructs. - var result float32 + lexer.SetAnalyser(func(text string) float32 { + // try to identify OpenEdge ABL based on a few common constructs. + var result float32 - if strings.Contains(text, "END.") { - result += 0.05 - } + if strings.Contains(text, "END.") { + result += 0.05 + } - if strings.Contains(text, "END PROCEDURE.") { - result += 0.05 - } + if strings.Contains(text, "END PROCEDURE.") { + result += 0.05 + } - if strings.Contains(text, "ELSE DO:") { - result += 0.05 - } + if strings.Contains(text, "ELSE DO:") { + result += 0.05 + } - return result - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (OpenEdgeABL) Name() string { - return heartbeat.LanguageOpenEdgeABL.StringChroma() + return result + }) } diff --git a/pkg/lexer/openedgeabl_test.go b/pkg/lexer/openedgeabl_test.go old mode 100644 new mode 100755 index 90d94651..97f45223 --- a/pkg/lexer/openedgeabl_test.go +++ b/pkg/lexer/openedgeabl_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestOpenEdge_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestOpenEdge_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.OpenEdgeABL{}.Lexer() + l := lexers.Get(heartbeat.LanguageOpenEdgeABL.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/pan.go b/pkg/lexer/pan.go index 4f022923..068c2638 100644 --- a/pkg/lexer/pan.go +++ b/pkg/lexer/pan.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Pan lexer. -type Pan struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePan.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Pan) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"pan"}, Filenames: []string{"*.pan"}, }, @@ -22,10 +29,5 @@ func (l Pan) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Pan) Name() string { - return heartbeat.LanguagePan.StringChroma() + )) } diff --git a/pkg/lexer/parasail.go b/pkg/lexer/parasail.go index 48c9b1d9..22ee7c9c 100644 --- a/pkg/lexer/parasail.go +++ b/pkg/lexer/parasail.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ParaSail lexer. -type ParaSail struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageParaSail.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ParaSail) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"parasail"}, Filenames: []string{"*.psi", "*.psl"}, MimeTypes: []string{"text/x-parasail"}, @@ -23,10 +30,5 @@ func (l ParaSail) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ParaSail) Name() string { - return heartbeat.LanguageParaSail.StringChroma() + )) } diff --git a/pkg/lexer/pawn.go b/pkg/lexer/pawn.go index 17608967..a6232279 100644 --- a/pkg/lexer/pawn.go +++ b/pkg/lexer/pawn.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Pawn lexer. -type Pawn struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePawn.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Pawn) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"pawn"}, Filenames: []string{"*.p", "*.pwn", "*.inc"}, MimeTypes: []string{"text/x-pawn"}, @@ -25,9 +32,7 @@ func (l Pawn) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // This is basically C. There is a keyword which doesn't exist in C // though and is nearly unique to this language. if strings.Contains(text, "tagof") { @@ -35,12 +40,5 @@ func (l Pawn) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Pawn) Name() string { - return heartbeat.LanguagePawn.StringChroma() + })) } diff --git a/pkg/lexer/pawn_test.go b/pkg/lexer/pawn_test.go old mode 100644 new mode 100755 index 0d4807a7..3ed720f2 --- a/pkg/lexer/pawn_test.go +++ b/pkg/lexer/pawn_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPawn_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/pawn_tagof.pwn") assert.NoError(t, err) - l := lexer.Pawn{}.Lexer() + l := lexers.Get(heartbeat.LanguagePawn.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.01), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/peg.go b/pkg/lexer/peg.go index d63a701c..ee95415b 100644 --- a/pkg/lexer/peg.go +++ b/pkg/lexer/peg.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// PEG lexer. -type PEG struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePEG.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l PEG) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"peg"}, Filenames: []string{"*.peg"}, MimeTypes: []string{"text/x-peg"}, @@ -23,10 +30,5 @@ func (l PEG) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (PEG) Name() string { - return heartbeat.LanguagePEG.StringChroma() + )) } diff --git a/pkg/lexer/perl.go b/pkg/lexer/perl.go index 2bdd03ab..e24e530f 100644 --- a/pkg/lexer/perl.go +++ b/pkg/lexer/perl.go @@ -5,52 +5,41 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/shebang" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) var perlAnalyserRe = regexp.MustCompile(`(?:my|our)\s+[$@%(]`) -// Perl lexer. -type Perl struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePerl.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Perl) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if matched, _ := shebang.MatchString(text, "perl"); matched { - return 1.0 - } + lexer.SetAnalyser(func(text string) float32 { + if matched, _ := shebang.MatchString(text, "perl"); matched { + return 1.0 + } - var result float32 + var result float32 - if perlAnalyserRe.MatchString(text) { - result += 0.9 - } + if perlAnalyserRe.MatchString(text) { + result += 0.9 + } - if strings.Contains(text, ":=") { - // := is not valid Perl, but it appears in unicon, so we should - // become less confident if we think we found Perl with := - result /= 2 - } + if strings.Contains(text, ":=") { + // := is not valid Perl, but it appears in unicon, so we should + // become less confident if we think we found Perl with := + result /= 2 + } - return result - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Perl) Name() string { - return heartbeat.LanguagePerl.StringChroma() + return result + }) } diff --git a/pkg/lexer/perl6.go b/pkg/lexer/perl6.go index 9c8f0bd9..b860d215 100644 --- a/pkg/lexer/perl6.go +++ b/pkg/lexer/perl6.go @@ -5,9 +5,11 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/shebang" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -20,17 +22,38 @@ var ( perl6EndPodRe = regexp.MustCompile(`^=(?:end|cut)`) ) -// Perl6 lexer. -type Perl6 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePerl6.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Perl6) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + // Roku uses Perl6 as alias + if lexer != nil && lexer.Config().Name != "Raku" { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"perl6", "pl6", "raku"}, - Filenames: []string{"*.pl", "*.pm", "*.nqp", "*.p6", "*.6pl", "*.p6l", "*.pl6", - "*.6pm", "*.p6m", "*.pm6", "*.t", "*.raku", "*.rakumod", "*.rakutest", "*.rakudoc"}, + Filenames: []string{ + "*.pl", + "*.pm", + "*.nqp", + "*.p6", + "*.6pl", + "*.p6l", + "*.pl6", + "*.6pm", + "*.p6m", + "*.pm6", + "*.t", + "*.raku", + "*.rakumod", + "*.rakutest", + "*.rakudoc", + }, MimeTypes: []string{"text/x-perl6", "application/x-perl6"}, }, func() chroma.Rules { @@ -38,9 +61,7 @@ func (l Perl6) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if matched, _ := shebang.MatchString(text, "perl6|rakudo|niecza|pugs"); matched { return 1.0 } @@ -90,9 +111,7 @@ func (l Perl6) Lexer() chroma.Lexer { } return result - }) - - return lexer + })) } func perl6StripPod(text string) []string { @@ -133,8 +152,3 @@ func perl6GetSubgroups(match []string) map[string]string { return groups } - -// Name returns the name of the lexer. -func (Perl6) Name() string { - return heartbeat.LanguagePerl6.StringChroma() -} diff --git a/pkg/lexer/perl6_test.go b/pkg/lexer/perl6_test.go old mode 100644 new mode 100755 index 0cc7b469..17f198e3 --- a/pkg/lexer/perl6_test.go +++ b/pkg/lexer/perl6_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPerl6_AnalyseText(t *testing.T) { @@ -45,7 +47,8 @@ func TestPerl6_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Perl6{}.Lexer() + l := lexers.Get(heartbeat.LanguagePerl6.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/perl_test.go b/pkg/lexer/perl_test.go old mode 100644 new mode 100755 index bc2e208b..7e986d26 --- a/pkg/lexer/perl_test.go +++ b/pkg/lexer/perl_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPerl_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestPerl_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Perl{}.Lexer() + l := lexers.Get(heartbeat.LanguagePerl.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/pike.go b/pkg/lexer/pike.go index 362a5b96..5e6122c7 100644 --- a/pkg/lexer/pike.go +++ b/pkg/lexer/pike.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Pike lexer. -type Pike struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePike.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Pike) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"pike"}, Filenames: []string{"*.pike", "*.pmod"}, MimeTypes: []string{"text/x-pike"}, @@ -23,10 +30,5 @@ func (l Pike) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Pike) Name() string { - return heartbeat.LanguagePike.StringChroma() + )) } diff --git a/pkg/lexer/pointless.go b/pkg/lexer/pointless.go index c5b9d0c0..ec887ae4 100644 --- a/pkg/lexer/pointless.go +++ b/pkg/lexer/pointless.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Pointless lexer. -type Pointless struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePointless.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Pointless) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"pointless"}, Filenames: []string{"*.ptls"}, }, @@ -22,10 +29,5 @@ func (l Pointless) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Pointless) Name() string { - return heartbeat.LanguagePointless.StringChroma() + )) } diff --git a/pkg/lexer/povray.go b/pkg/lexer/povray.go index 0059ceac..9f4809b0 100644 --- a/pkg/lexer/povray.go +++ b/pkg/lexer/povray.go @@ -4,57 +4,46 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// POVRay lexer. -type POVRay struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePOVRay.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l POVRay) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - // POVRAY is similar to JSON/C, but the combination of camera and - // light_source is probably not very likely elsewhere. HLSL or GLSL - // are similar (GLSL even has #version), but they miss #declare, and - // light_source/camera are not keywords anywhere else -- it's fair - // to assume though that any POVRAY scene must have a camera and - // lightsource. - var result float32 - - if strings.Contains(text, "#version") { - result += 0.05 - } - - if strings.Contains(text, "#declare") { - result += 0.05 - } - - if strings.Contains(text, "camera") { - result += 0.05 - } - - if strings.Contains(text, "light_source") { - result += 0.1 - } - - return result - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (POVRay) Name() string { - return heartbeat.LanguagePOVRay.StringChroma() + lexer.SetAnalyser(func(text string) float32 { + // POVRAY is similar to JSON/C, but the combination of camera and + // light_source is probably not very likely elsewhere. HLSL or GLSL + // are similar (GLSL even has #version), but they miss #declare, and + // light_source/camera are not keywords anywhere else -- it's fair + // to assume though that any POVRAY scene must have a camera and + // lightsource. + var result float32 + + if strings.Contains(text, "#version") { + result += 0.05 + } + + if strings.Contains(text, "#declare") { + result += 0.05 + } + + if strings.Contains(text, "camera") { + result += 0.05 + } + + if strings.Contains(text, "light_source") { + result += 0.1 + } + + return result + }) } diff --git a/pkg/lexer/povray_test.go b/pkg/lexer/povray_test.go old mode 100644 new mode 100755 index 3b8cd480..4be4fedb --- a/pkg/lexer/povray_test.go +++ b/pkg/lexer/povray_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPovRay_AnalyseText(t *testing.T) { @@ -37,7 +39,8 @@ func TestPovRay_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.POVRay{}.Lexer() + l := lexers.Get(heartbeat.LanguagePOVRay.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/powershellsession.go b/pkg/lexer/powershellsession.go index 608ef721..cab63156 100644 --- a/pkg/lexer/powershellsession.go +++ b/pkg/lexer/powershellsession.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// PowerShellSession lexer. -type PowerShellSession struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePowerShellSession.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l PowerShellSession) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ps1con"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l PowerShellSession) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (PowerShellSession) Name() string { - return heartbeat.LanguagePowerShellSession.StringChroma() + )) } diff --git a/pkg/lexer/praat.go b/pkg/lexer/praat.go index f20ed4df..342cf6ff 100644 --- a/pkg/lexer/praat.go +++ b/pkg/lexer/praat.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Praat lexer. -type Praat struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePraat.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Praat) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"praat"}, Filenames: []string{"*.praat", "*.proc", "*.psc"}, }, @@ -22,10 +29,5 @@ func (l Praat) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Praat) Name() string { - return heartbeat.LanguagePraat.StringChroma() + )) } diff --git a/pkg/lexer/processing.go b/pkg/lexer/processing.go index 2f900413..758d38a0 100644 --- a/pkg/lexer/processing.go +++ b/pkg/lexer/processing.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Processing lexer. -type Processing struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageProcessing.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Processing) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"processing"}, Filenames: []string{"*.pde"}, }, @@ -22,10 +29,5 @@ func (l Processing) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Processing) Name() string { - return heartbeat.LanguageProcessing.StringChroma() + )) } diff --git a/pkg/lexer/prolog.go b/pkg/lexer/prolog.go index 072e391f..f90ac033 100644 --- a/pkg/lexer/prolog.go +++ b/pkg/lexer/prolog.go @@ -4,37 +4,26 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// Prolog lexer. -type Prolog struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageProlog.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Prolog) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if strings.Contains(text, ":-") { - return 1.0 - } + lexer.SetAnalyser(func(text string) float32 { + if strings.Contains(text, ":-") { + return 1.0 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Prolog) Name() string { - return heartbeat.LanguageProlog.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/prolog_test.go b/pkg/lexer/prolog_test.go old mode 100644 new mode 100755 index 6989f4ce..442d9972 --- a/pkg/lexer/prolog_test.go +++ b/pkg/lexer/prolog_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestProlog_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/prolog.ecl") assert.NoError(t, err) - l := lexer.Prolog{}.Lexer() + l := lexers.Get(heartbeat.LanguageProlog.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/psql.go b/pkg/lexer/psql.go index 99663421..40ca2c8a 100644 --- a/pkg/lexer/psql.go +++ b/pkg/lexer/psql.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// PostgresConsole lexer. -type PostgresConsole struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePostgresConsole.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l PostgresConsole) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"psql", "postgresql-console", "postgres-console"}, + Name: language, + Aliases: []string{"psql", "postgresql-console", "postgres-console"}, + MimeTypes: []string{"text/x-postgresql-psql"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l PostgresConsole) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (PostgresConsole) Name() string { - return heartbeat.LanguagePostgresConsole.StringChroma() + )) } diff --git a/pkg/lexer/psyshphp.go b/pkg/lexer/psyshphp.go index 9cb4c94c..8dec3f4a 100644 --- a/pkg/lexer/psyshphp.go +++ b/pkg/lexer/psyshphp.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// PsyShPHP lexer. -type PsyShPHP struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePsyShPHP.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l PsyShPHP) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"psysh"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l PsyShPHP) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (PsyShPHP) Name() string { - return heartbeat.LanguagePsyShPHP.StringChroma() + )) } diff --git a/pkg/lexer/pug.go b/pkg/lexer/pug.go index 67e3ff2a..e5051a5c 100644 --- a/pkg/lexer/pug.go +++ b/pkg/lexer/pug.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Pug lexer. -type Pug struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePug.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Pug) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"pug", "jade"}, Filenames: []string{"*.pug", "*.jade"}, MimeTypes: []string{"text/x-pug", "text/x-jade"}, @@ -23,10 +30,5 @@ func (l Pug) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Pug) Name() string { - return heartbeat.LanguagePug.StringChroma() + )) } diff --git a/pkg/lexer/pypylog.go b/pkg/lexer/pypylog.go index 7c26ca3e..587734b1 100644 --- a/pkg/lexer/pypylog.go +++ b/pkg/lexer/pypylog.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// PyPyLog lexer. -type PyPyLog struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePyPyLog.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l PyPyLog) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"pypylog", "pypy"}, Filenames: []string{"*.pypylog"}, MimeTypes: []string{"application/x-pypylog"}, @@ -23,10 +30,5 @@ func (l PyPyLog) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (PyPyLog) Name() string { - return heartbeat.LanguagePyPyLog.StringChroma() + )) } diff --git a/pkg/lexer/python.go b/pkg/lexer/python.go index 17738441..bb0d48b9 100644 --- a/pkg/lexer/python.go +++ b/pkg/lexer/python.go @@ -4,44 +4,33 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/shebang" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// Python lexer. -type Python struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePython.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Python) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - matched, _ := shebang.MatchString(text, `pythonw?(3(\.\d)?)?`) + lexer.SetAnalyser(func(text string) float32 { + matched, _ := shebang.MatchString(text, `pythonw?(3(\.\d)?)?`) - if len(text) > 1000 { - text = text[:1000] - } + if len(text) > 1000 { + text = text[:1000] + } - if matched || strings.Contains(text, "import ") { - return 1.0 - } + if matched || strings.Contains(text, "import ") { + return 1.0 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Python) Name() string { - return heartbeat.LanguagePython.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/python2.go b/pkg/lexer/python2.go index 58ac675c..59e14259 100644 --- a/pkg/lexer/python2.go +++ b/pkg/lexer/python2.go @@ -2,38 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/shebang" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// Python2 lexer. -type Python2 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePython2.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Python2) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if matched, _ := shebang.MatchString(text, `pythonw?2(\.\d)?`); matched { - return 1.0 - } + lexer.SetAnalyser(func(text string) float32 { + if matched, _ := shebang.MatchString(text, `pythonw?2(\.\d)?`); matched { + return 1.0 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Python2) Name() string { - return heartbeat.LanguagePython2.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/python2_test.go b/pkg/lexer/python2_test.go old mode 100644 new mode 100755 index d8881795..9758c3c7 --- a/pkg/lexer/python2_test.go +++ b/pkg/lexer/python2_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPython2_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/python2_shebang.py") assert.NoError(t, err) - l := lexer.Python2{}.Lexer() + l := lexers.Get(heartbeat.LanguagePython2.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/python2traceback.go b/pkg/lexer/python2traceback.go index 3a55a428..57409d2c 100644 --- a/pkg/lexer/python2traceback.go +++ b/pkg/lexer/python2traceback.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Python2Traceback lexer. -type Python2Traceback struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePython2Traceback.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Python2Traceback) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"py2tb"}, Filenames: []string{"*.py2tb"}, MimeTypes: []string{"text/x-python2-traceback"}, @@ -23,10 +30,5 @@ func (l Python2Traceback) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Python2Traceback) Name() string { - return heartbeat.LanguagePython2Traceback.StringChroma() + )) } diff --git a/pkg/lexer/python_test.go b/pkg/lexer/python_test.go old mode 100644 new mode 100755 index de907e10..3202cc70 --- a/pkg/lexer/python_test.go +++ b/pkg/lexer/python_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPython_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestPython_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Python{}.Lexer() + l := lexers.Get(heartbeat.LanguagePython.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/pythonconsole.go b/pkg/lexer/pythonconsole.go index d4caccbf..78ff9b1d 100644 --- a/pkg/lexer/pythonconsole.go +++ b/pkg/lexer/pythonconsole.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// PythonConsole lexer. -type PythonConsole struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePythonConsole.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l PythonConsole) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"pycon"}, + Name: language, + Aliases: []string{"pycon"}, + MimeTypes: []string{"text/x-python-doctest"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l PythonConsole) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (PythonConsole) Name() string { - return heartbeat.LanguagePythonConsole.StringChroma() + )) } diff --git a/pkg/lexer/pythontraceback.go b/pkg/lexer/pythontraceback.go index 9b9c765f..acb0c2de 100644 --- a/pkg/lexer/pythontraceback.go +++ b/pkg/lexer/pythontraceback.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// PythonTraceback lexer. -type PythonTraceback struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguagePythonTraceback.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l PythonTraceback) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"pytb", "py3tb"}, Filenames: []string{"*.pytb", "*.py3tb"}, MimeTypes: []string{"text/x-python-traceback", "text/x-python3-traceback"}, @@ -23,10 +30,5 @@ func (l PythonTraceback) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (PythonTraceback) Name() string { - return heartbeat.LanguagePythonTraceback.StringChroma() + )) } diff --git a/pkg/lexer/qbasic.go b/pkg/lexer/qbasic.go index 8120f2dc..d1b1742c 100644 --- a/pkg/lexer/qbasic.go +++ b/pkg/lexer/qbasic.go @@ -4,37 +4,26 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// QBasic lexer. -type QBasic struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageQBasic.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l QBasic) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if strings.Contains(text, "$DYNAMIC") || strings.Contains(text, "$STATIC") { - return 0.9 - } + lexer.SetAnalyser(func(text string) float32 { + if strings.Contains(text, "$DYNAMIC") || strings.Contains(text, "$STATIC") { + return 0.9 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (QBasic) Name() string { - return heartbeat.LanguageQBasic.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/qbasic_test.go b/pkg/lexer/qbasic_test.go old mode 100644 new mode 100755 index 2d5c6d5e..415051d1 --- a/pkg/lexer/qbasic_test.go +++ b/pkg/lexer/qbasic_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestQBasic_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestQBasic_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.QBasic{}.Lexer() + l := lexers.Get(heartbeat.LanguageQBasic.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/qvto.go b/pkg/lexer/qvto.go index 010d441d..4a7f79b6 100644 --- a/pkg/lexer/qvto.go +++ b/pkg/lexer/qvto.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// QVTO lexer. For the QVT Operational Mapping language . -type QVTO struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageQVTO.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l QVTO) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"qvto", "qvt"}, Filenames: []string{"*.qvto"}, }, @@ -22,10 +29,5 @@ func (l QVTO) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (QVTO) Name() string { - return heartbeat.LanguageQVTO.StringChroma() + )) } diff --git a/pkg/lexer/r.go b/pkg/lexer/r.go index 2d086036..891932a7 100644 --- a/pkg/lexer/r.go +++ b/pkg/lexer/r.go @@ -2,8 +2,8 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" "github.com/dlclark/regexp2" ) @@ -11,33 +11,22 @@ import ( // nolint:gochecknoglobals var rAnalyzerRe = regexp2.MustCompile(`[a-z0-9_\])\s]<-(?!-)`, regexp2.None) -// R and also S lexer. -type R struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageR.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l R) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - matched, _ := rAnalyzerRe.MatchString(text) - if matched { - return 0.11 - } + lexer.SetAnalyser(func(text string) float32 { + matched, _ := rAnalyzerRe.MatchString(text) + if matched { + return 0.11 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (R) Name() string { - return heartbeat.LanguageR.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/r_test.go b/pkg/lexer/r_test.go old mode 100644 new mode 100755 index 212f4616..2ab0efe2 --- a/pkg/lexer/r_test.go +++ b/pkg/lexer/r_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestR_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/r_expression.r") assert.NoError(t, err) - l := lexer.R{}.Lexer() + l := lexers.Get(heartbeat.LanguageR.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.11), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/ragelembedded.go b/pkg/lexer/ragelembedded.go index b615c70c..e068cfcc 100644 --- a/pkg/lexer/ragelembedded.go +++ b/pkg/lexer/ragelembedded.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RagelEmbedded lexer. A lexer for Ragel embedded in a host language file. -type RagelEmbedded struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRagelEmbedded.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RagelEmbedded) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ragel-em"}, Filenames: []string{"*.rl"}, }, @@ -24,20 +31,11 @@ func (l RagelEmbedded) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if strings.Contains(text, "@LANG: indep") { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (RagelEmbedded) Name() string { - return heartbeat.LanguageRagelEmbedded.StringChroma() + })) } diff --git a/pkg/lexer/ragelembedded_test.go b/pkg/lexer/ragelembedded_test.go old mode 100644 new mode 100755 index 8fe99ab4..7ca066a1 --- a/pkg/lexer/ragelembedded_test.go +++ b/pkg/lexer/ragelembedded_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRagelEmbedded_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/ragel.rl") assert.NoError(t, err) - l := lexer.RagelEmbedded{}.Lexer() + l := lexers.Get(heartbeat.LanguageRagelEmbedded.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/rawtoken.go b/pkg/lexer/rawtoken.go index 67c4dfa0..4ba022d2 100644 --- a/pkg/lexer/rawtoken.go +++ b/pkg/lexer/rawtoken.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RawToken lexer. -type RawToken struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRawToken.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RawToken) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"raw"}, + Name: language, + Aliases: []string{"raw"}, + MimeTypes: []string{"application/x-pygments-tokens"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l RawToken) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RawToken) Name() string { - return heartbeat.LanguageRawToken.StringChroma() + )) } diff --git a/pkg/lexer/razor.go b/pkg/lexer/razor.go index 2bc5298a..40e65867 100644 --- a/pkg/lexer/razor.go +++ b/pkg/lexer/razor.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Razor lexer. Lexer for Blazor's Razor files. -type Razor struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRazor.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Razor) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"razor"}, Filenames: []string{"*.razor"}, MimeTypes: []string{"text/html"}, @@ -23,10 +30,5 @@ func (l Razor) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Razor) Name() string { - return heartbeat.LanguageRazor.StringChroma() + )) } diff --git a/pkg/lexer/rconsole.go b/pkg/lexer/rconsole.go index a1222be2..128eb755 100644 --- a/pkg/lexer/rconsole.go +++ b/pkg/lexer/rconsole.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RConsole lexer. For R console transcripts or R CMD BATCH output files. -type RConsole struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRConsole.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RConsole) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rconsole", "rout"}, Filenames: []string{"*.Rout"}, }, @@ -22,10 +29,5 @@ func (l RConsole) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RConsole) Name() string { - return heartbeat.LanguageRConsole.StringChroma() + )) } diff --git a/pkg/lexer/rd.go b/pkg/lexer/rd.go index 9f694ccc..8ca415e4 100644 --- a/pkg/lexer/rd.go +++ b/pkg/lexer/rd.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Rd lexer. Lexer for R documentation (Rd) files. -type Rd struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRd.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Rd) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rd"}, Filenames: []string{"*.Rd"}, MimeTypes: []string{"text/x-r-doc"}, @@ -23,10 +30,5 @@ func (l Rd) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Rd) Name() string { - return heartbeat.LanguageRd.StringChroma() + )) } diff --git a/pkg/lexer/rebol.go b/pkg/lexer/rebol.go index 25d0212e..8a04bf0d 100644 --- a/pkg/lexer/rebol.go +++ b/pkg/lexer/rebol.go @@ -4,8 +4,10 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -13,14 +15,19 @@ var ( rebolAnalyserHeaderPrecedingTextRe = regexp.MustCompile(`\s*REBOL\s*\[`) ) -// REBOL lexer. -type REBOL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageREBOL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l REBOL) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rebol"}, Filenames: []string{"*.r", "*.r3", "*.reb"}, MimeTypes: []string{"text/x-rebol"}, @@ -30,9 +37,7 @@ func (l REBOL) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // Check if code contains REBOL header, then it's probably not R code if rebolAnalyserHeaderRe.MatchString(text) { return 1.0 @@ -43,12 +48,5 @@ func (l REBOL) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (REBOL) Name() string { - return heartbeat.LanguageREBOL.StringChroma() + })) } diff --git a/pkg/lexer/rebol_test.go b/pkg/lexer/rebol_test.go old mode 100644 new mode 100755 index 27f8a355..32f1c4d4 --- a/pkg/lexer/rebol_test.go +++ b/pkg/lexer/rebol_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestREBOL_AnalyseText(t *testing.T) { @@ -30,7 +32,8 @@ func TestREBOL_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.REBOL{}.Lexer() + l := lexers.Get(heartbeat.LanguageREBOL.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/red.go b/pkg/lexer/red.go index f6c8d007..aa29728d 100644 --- a/pkg/lexer/red.go +++ b/pkg/lexer/red.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Red lexer. A Red-language lexer. -type Red struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRed.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Red) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"red", "red/system"}, Filenames: []string{"*.red", "*.reds"}, MimeTypes: []string{"text/x-red", "text/x-red-system"}, @@ -23,10 +30,5 @@ func (l Red) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Red) Name() string { - return heartbeat.LanguageRed.StringChroma() + )) } diff --git a/pkg/lexer/redcode.go b/pkg/lexer/redcode.go index 876c4d88..9fa0c545 100644 --- a/pkg/lexer/redcode.go +++ b/pkg/lexer/redcode.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Redcode lexer. -type Redcode struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRedcode.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Redcode) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"redcode"}, Filenames: []string{"*.cw"}, }, @@ -22,10 +29,5 @@ func (l Redcode) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Redcode) Name() string { - return heartbeat.LanguageRedcode.StringChroma() + )) } diff --git a/pkg/lexer/rescript.go b/pkg/lexer/rescript.go index 7476cbea..98ccc671 100644 --- a/pkg/lexer/rescript.go +++ b/pkg/lexer/rescript.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ReScript lexer. -type ReScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageReScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ReScript) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rescript"}, Filenames: []string{"*.res", "*.resi"}, MimeTypes: []string{"text/x-rescript"}, @@ -23,10 +30,5 @@ func (l ReScript) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ReScript) Name() string { - return heartbeat.LanguageReScript.StringChroma() + )) } diff --git a/pkg/lexer/resourcebundle.go b/pkg/lexer/resourcebundle.go index a0e3800a..a7c7615b 100644 --- a/pkg/lexer/resourcebundle.go +++ b/pkg/lexer/resourcebundle.go @@ -4,19 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ResourceBundle lexer. Lexer for ICU ResourceBundle bundles -// -type ResourceBundle struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageResourceBundle.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ResourceBundle) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"resource", "resourcebundle"}, }, func() chroma.Rules { @@ -24,20 +30,11 @@ func (l ResourceBundle) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if strings.HasPrefix(text, "root:table") { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (ResourceBundle) Name() string { - return heartbeat.LanguageResourceBundle.StringChroma() + })) } diff --git a/pkg/lexer/resourcebundle_test.go b/pkg/lexer/resourcebundle_test.go old mode 100644 new mode 100755 index 6ed6f2b1..653e6f3a --- a/pkg/lexer/resourcebundle_test.go +++ b/pkg/lexer/resourcebundle_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestResourceBundle_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/resource.txt") assert.NoError(t, err) - l := lexer.ResourceBundle{}.Lexer() + l := lexers.Get(heartbeat.LanguageResourceBundle.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/rhtml.go b/pkg/lexer/rhtml.go index 0fa0604d..d5fe1ab7 100644 --- a/pkg/lexer/rhtml.go +++ b/pkg/lexer/rhtml.go @@ -3,19 +3,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/doctype" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RHTML lexer. Subclass of the ERB lexer that highlights the unlexed data -// with the html lexer. -type RHTML struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRHTML.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RHTML) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rhtml", "html+erb", "html+ruby"}, Filenames: []string{"*.rhtml"}, AliasFilenames: []string{"*.html", "*.htm", "*.xhtml"}, @@ -26,10 +32,13 @@ func (l RHTML) Lexer() chroma.Lexer { "root": {}, } }, - ) + ).SetAnalyser(func(text string) float32 { + erb := lexers.Get(heartbeat.LanguageERB.StringChroma()) + if erb == nil { + return 0 + } - lexer.SetAnalyser(func(text string) float32 { - result := ERB{}.Lexer().AnalyseText(text) - 0.01 + result := erb.AnalyseText(text) - 0.01 if matched, _ := doctype.MatchString(text, "html"); matched { // one more than the XmlErbLexer returns @@ -37,12 +46,5 @@ func (l RHTML) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (RHTML) Name() string { - return heartbeat.LanguageRHTML.StringChroma() + })) } diff --git a/pkg/lexer/rhtml_test.go b/pkg/lexer/rhtml_test.go old mode 100644 new mode 100755 index a53123f2..33daca08 --- a/pkg/lexer/rhtml_test.go +++ b/pkg/lexer/rhtml_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRHTML_AnalyseText(t *testing.T) { @@ -30,7 +32,8 @@ func TestRHTML_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.RHTML{}.Lexer() + l := lexers.Get(heartbeat.LanguageRHTML.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/ride.go b/pkg/lexer/ride.go index 272fcd8b..5b185027 100644 --- a/pkg/lexer/ride.go +++ b/pkg/lexer/ride.go @@ -2,19 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Ride lexer. For Ride -// source code. -type Ride struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRide.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Ride) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ride"}, Filenames: []string{"*.ride"}, MimeTypes: []string{"text/x-ride"}, @@ -24,10 +30,5 @@ func (l Ride) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Ride) Name() string { - return heartbeat.LanguageRide.StringChroma() + )) } diff --git a/pkg/lexer/rngcompact.go b/pkg/lexer/rngcompact.go index 8eb18090..4eb604f9 100644 --- a/pkg/lexer/rngcompact.go +++ b/pkg/lexer/rngcompact.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RNGCompact lexer. For RelaxNG-compact syntax. -type RNGCompact struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRNGCompact.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RNGCompact) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rnc", "rng-compact"}, Filenames: []string{"*.rnc"}, }, @@ -22,10 +29,5 @@ func (l RNGCompact) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RNGCompact) Name() string { - return heartbeat.LanguageRNGCompact.StringChroma() + )) } diff --git a/pkg/lexer/roboconfgraph.go b/pkg/lexer/roboconfgraph.go index 5e095c59..c3e9dc47 100644 --- a/pkg/lexer/roboconfgraph.go +++ b/pkg/lexer/roboconfgraph.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RoboconfGraph lexer for Roboconf graph files. -type RoboconfGraph struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRoboconfGraph.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RoboconfGraph) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"roboconf-graph"}, Filenames: []string{"*.graph"}, }, @@ -22,10 +29,5 @@ func (l RoboconfGraph) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RoboconfGraph) Name() string { - return heartbeat.LanguageRoboconfGraph.StringChroma() + )) } diff --git a/pkg/lexer/roboconfinstances.go b/pkg/lexer/roboconfinstances.go index d7962e3f..c0602889 100644 --- a/pkg/lexer/roboconfinstances.go +++ b/pkg/lexer/roboconfinstances.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RoboconfInstances lexer for Roboconf instances files. -type RoboconfInstances struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRoboconfInstances.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RoboconfInstances) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"roboconf-instances"}, Filenames: []string{"*.instances"}, }, @@ -22,10 +29,5 @@ func (l RoboconfInstances) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RoboconfInstances) Name() string { - return heartbeat.LanguageRoboconfInstances.StringChroma() + )) } diff --git a/pkg/lexer/robotframework.go b/pkg/lexer/robotframework.go index 54352258..bd4d797e 100644 --- a/pkg/lexer/robotframework.go +++ b/pkg/lexer/robotframework.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RobotFramework lexer for Robot Framework test data. -type RobotFramework struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRobotFramework.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RobotFramework) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"robotframework"}, Filenames: []string{"*.robot"}, MimeTypes: []string{"text/x-robotframework"}, @@ -23,10 +30,5 @@ func (l RobotFramework) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RobotFramework) Name() string { - return heartbeat.LanguageRobotFramework.StringChroma() + )) } diff --git a/pkg/lexer/rpmspec.go b/pkg/lexer/rpmspec.go index 6dd89afa..c839396f 100644 --- a/pkg/lexer/rpmspec.go +++ b/pkg/lexer/rpmspec.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RPMSpec lexer. -type RPMSpec struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRPMSpec.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RPMSpec) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"spec"}, Filenames: []string{"*.spec"}, MimeTypes: []string{"text/x-rpm-spec"}, @@ -23,10 +30,5 @@ func (l RPMSpec) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RPMSpec) Name() string { - return heartbeat.LanguageRPMSpec.StringChroma() + )) } diff --git a/pkg/lexer/rql.go b/pkg/lexer/rql.go index 4e38fe6a..318e3fc9 100644 --- a/pkg/lexer/rql.go +++ b/pkg/lexer/rql.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RQL lexer for Relation Query Language -type RQL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRQL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RQL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rql"}, Filenames: []string{"*.rql"}, MimeTypes: []string{"text/x-rql"}, @@ -23,10 +30,5 @@ func (l RQL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RQL) Name() string { - return heartbeat.LanguageRQL.StringChroma() + )) } diff --git a/pkg/lexer/rsl.go b/pkg/lexer/rsl.go index 02178329..22e0d014 100644 --- a/pkg/lexer/rsl.go +++ b/pkg/lexer/rsl.go @@ -4,22 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var rslAnalyserRe = regexp.MustCompile(`(?i)scheme\s*.*?=\s*class\s*type`) -// RSL lexer. RSL is the formal -// specification language used in RAISE (Rigorous Approach to Industrial -// Software Engineering) method. -type RSL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRSL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RSL) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rsl"}, Filenames: []string{"*.rsl"}, MimeTypes: []string{"text/rsl"}, @@ -29,21 +34,12 @@ func (l RSL) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // Check for the most common text in the beginning of a RSL file. if rslAnalyserRe.MatchString(text) { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (RSL) Name() string { - return heartbeat.LanguageRSL.StringChroma() + })) } diff --git a/pkg/lexer/rsl_test.go b/pkg/lexer/rsl_test.go old mode 100644 new mode 100755 index 106de3fa..abf29f4b --- a/pkg/lexer/rsl_test.go +++ b/pkg/lexer/rsl_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRSL_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/raise.rsl") assert.NoError(t, err) - l := lexer.RSL{}.Lexer() + l := lexers.Get(heartbeat.LanguageRSL.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/rubyirbsession.go b/pkg/lexer/rubyirbsession.go index 0cb8a52d..7461f5e2 100644 --- a/pkg/lexer/rubyirbsession.go +++ b/pkg/lexer/rubyirbsession.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// RubyIRBSession lexer. For Ruby interactive console (**irb**) output. -type RubyIRBSession struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageRubyIRBSession.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l RubyIRBSession) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"rbcon", "irb"}, + Name: language, + Aliases: []string{"rbcon", "irb"}, + MimeTypes: []string{"text/x-ruby-shellsession"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l RubyIRBSession) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (RubyIRBSession) Name() string { - return heartbeat.LanguageRubyIRBSession.StringChroma() + )) } diff --git a/pkg/lexer/sarl.go b/pkg/lexer/sarl.go index 49775d4d..c33bfc86 100644 --- a/pkg/lexer/sarl.go +++ b/pkg/lexer/sarl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// SARL lexer. For SARL source code. -type SARL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSARL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l SARL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"sarl"}, Filenames: []string{"*.sarl"}, MimeTypes: []string{"text/x-sarl"}, @@ -23,10 +30,5 @@ func (l SARL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (SARL) Name() string { - return heartbeat.LanguageSARL.StringChroma() + )) } diff --git a/pkg/lexer/scaml.go b/pkg/lexer/scaml.go index a16ab4a5..8e8959d9 100644 --- a/pkg/lexer/scaml.go +++ b/pkg/lexer/scaml.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Scaml lexer. For Scaml markup . Scaml is Haml for Scala. -type Scaml struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageScaml.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Scaml) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"scaml"}, Filenames: []string{"*.scaml"}, MimeTypes: []string{"text/x-scaml"}, @@ -23,10 +30,5 @@ func (l Scaml) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Scaml) Name() string { - return heartbeat.LanguageScaml.StringChroma() + )) } diff --git a/pkg/lexer/scdoc.go b/pkg/lexer/scdoc.go index 6d7d91ad..0475bb50 100644 --- a/pkg/lexer/scdoc.go +++ b/pkg/lexer/scdoc.go @@ -4,30 +4,34 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Scdoc lexer. -type Scdoc struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageScdoc.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Scdoc) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"scdoc", "scd"}, Filenames: []string{"*.scd", "*.scdoc"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // This is very similar to markdown, save for the escape characters // needed for * and _. var result float32 @@ -41,12 +45,5 @@ func (l Scdoc) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Scdoc) Name() string { - return heartbeat.LanguageScdoc.StringChroma() + })) } diff --git a/pkg/lexer/scdoc_test.go b/pkg/lexer/scdoc_test.go old mode 100644 new mode 100755 index af99d2d8..834cb5ba --- a/pkg/lexer/scdoc_test.go +++ b/pkg/lexer/scdoc_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestScdoc_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestScdoc_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Scdoc{}.Lexer() + l := lexers.Get(heartbeat.LanguageScdoc.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/sgf.go b/pkg/lexer/sgf.go index da1f52d7..ff377f07 100644 --- a/pkg/lexer/sgf.go +++ b/pkg/lexer/sgf.go @@ -2,22 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// SmartGameFormat lexer. Lexer for Smart Game Format (sgf) file format. -// -// The format is used to store game records of board games for two players -// (mainly Go game). For more information about the definition of the format, -// see: https://www.red-bean.com/sgf/ -type SmartGameFormat struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSmartGameFormat.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l SmartGameFormat) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"sgf"}, Filenames: []string{"*.sgf"}, }, @@ -26,10 +29,5 @@ func (l SmartGameFormat) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (SmartGameFormat) Name() string { - return heartbeat.LanguageSmartGameFormat.StringChroma() + )) } diff --git a/pkg/lexer/shen.go b/pkg/lexer/shen.go index ca71a72f..4b318878 100644 --- a/pkg/lexer/shen.go +++ b/pkg/lexer/shen.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Shen lexer. Lexer for Shen source code. -type Shen struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageShen.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Shen) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"shen"}, Filenames: []string{"*.shen"}, MimeTypes: []string{"text/x-shen", "application/x-shen"}, @@ -23,10 +30,5 @@ func (l Shen) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Shen) Name() string { - return heartbeat.LanguageShen.StringChroma() + )) } diff --git a/pkg/lexer/shexc.go b/pkg/lexer/shexc.go index 1886343a..86f1cbe7 100644 --- a/pkg/lexer/shexc.go +++ b/pkg/lexer/shexc.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// ShExC lexer. Lexer for ShExC shape expressions language syntax. -type ShExC struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageShExC.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l ShExC) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"shexc", "shex"}, Filenames: []string{"*.shex"}, MimeTypes: []string{"text/shex"}, @@ -23,10 +30,5 @@ func (l ShExC) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (ShExC) Name() string { - return heartbeat.LanguageShExC.StringChroma() + )) } diff --git a/pkg/lexer/silver.go b/pkg/lexer/silver.go index a32b9d7f..f5d0159d 100644 --- a/pkg/lexer/silver.go +++ b/pkg/lexer/silver.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Silver lexer. For Silver source code. -type Silver struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSilver.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Silver) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"silver"}, Filenames: []string{"*.sil", "*.vpr"}, }, @@ -22,10 +29,5 @@ func (l Silver) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Silver) Name() string { - return heartbeat.LanguageSilver.StringChroma() + )) } diff --git a/pkg/lexer/singularity.go b/pkg/lexer/singularity.go index 412d0b7d..62a92897 100644 --- a/pkg/lexer/singularity.go +++ b/pkg/lexer/singularity.go @@ -3,8 +3,11 @@ package lexer import ( "regexp" - "github.com/alecthomas/chroma/v2" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" + + "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -13,26 +16,28 @@ var ( `%(?:pre|post|setup|environment|help|labels|test|runscript|files|startscript)\b`) ) -// Singularity lexer. -type Singularity struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSingularity.StringChroma() + lexer := lexers.Get(language) + + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } -// Lexer returns the lexer. -func (l Singularity) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"singularity"}, Filenames: []string{"*.def", "Singularity"}, - MimeTypes: []string{}, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // This is a quite simple script file, but there are a few keywords // which seem unique to this language. var result float32 @@ -46,12 +51,5 @@ func (l Singularity) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Singularity) Name() string { - return heartbeat.LanguageSingularity.StringChroma() + })) } diff --git a/pkg/lexer/singularity_test.go b/pkg/lexer/singularity_test.go old mode 100644 new mode 100755 index 4bcbc9a3..a0580c95 --- a/pkg/lexer/singularity_test.go +++ b/pkg/lexer/singularity_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSingularity_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestSingularity_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Singularity{}.Lexer() + l := lexers.Get(heartbeat.LanguageSingularity.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/sketchdrawing.go b/pkg/lexer/sketchdrawing.go index 00e81d3c..1a10d764 100644 --- a/pkg/lexer/sketchdrawing.go +++ b/pkg/lexer/sketchdrawing.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// SketchDrawing lexer. -type SketchDrawing struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSketchDrawing.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l SketchDrawing) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"sketch"}, Filenames: []string{"*.sketch"}, }, @@ -22,10 +29,5 @@ func (l SketchDrawing) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (SketchDrawing) Name() string { - return heartbeat.LanguageSketchDrawing.StringChroma() + )) } diff --git a/pkg/lexer/slash.go b/pkg/lexer/slash.go index 8bba59c4..5a14c721 100644 --- a/pkg/lexer/slash.go +++ b/pkg/lexer/slash.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Slash lexer. Lexer for the Slash programming language. -type Slash struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSlash.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Slash) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"slash"}, Filenames: []string{"*.sla"}, }, @@ -22,10 +29,5 @@ func (l Slash) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Slash) Name() string { - return heartbeat.LanguageSlash.StringChroma() + )) } diff --git a/pkg/lexer/slim.go b/pkg/lexer/slim.go index ac0fd53d..5390155b 100644 --- a/pkg/lexer/slim.go +++ b/pkg/lexer/slim.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Slim lexer. -type Slim struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSlim.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Slim) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"slim"}, Filenames: []string{"*.slim"}, MimeTypes: []string{"text/x-slim"}, @@ -23,10 +30,5 @@ func (l Slim) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Slim) Name() string { - return heartbeat.LanguageSlim.StringChroma() + )) } diff --git a/pkg/lexer/slint.go b/pkg/lexer/slint.go index 04044f69..4079b1b5 100644 --- a/pkg/lexer/slint.go +++ b/pkg/lexer/slint.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Slint lexer. Lexer for the Slint programming language. -type Slint struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSlint.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Slint) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"slint"}, Filenames: []string{"*.slint"}, }, @@ -22,10 +29,5 @@ func (l Slint) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Slint) Name() string { - return heartbeat.LanguageSlint.StringChroma() + )) } diff --git a/pkg/lexer/slurm.go b/pkg/lexer/slurm.go index 4f1b9d6c..f27735e3 100644 --- a/pkg/lexer/slurm.go +++ b/pkg/lexer/slurm.go @@ -2,19 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// Slurm lexer. Lexer for (ba|k|z|)sh Slurm scripts. -type Slurm struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSlurm.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Slurm) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"slurm", "sbatch"}, Filenames: []string{"*.sl"}, }, @@ -23,21 +29,12 @@ func (l Slurm) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { bash := lexers.Get(heartbeat.LanguageBash.StringChroma()) if bash == nil { return 0 } return bash.AnalyseText(text) - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Slurm) Name() string { - return heartbeat.LanguageSlurm.StringChroma() + })) } diff --git a/pkg/lexer/slurm_test.go b/pkg/lexer/slurm_test.go old mode 100644 new mode 100755 index 61432e77..46ca221d --- a/pkg/lexer/slurm_test.go +++ b/pkg/lexer/slurm_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSlurm_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/slurm.sl") assert.NoError(t, err) - l := lexer.Slurm{}.Lexer() + l := lexers.Get(heartbeat.LanguageSlurm.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/smali.go b/pkg/lexer/smali.go index 7614cc44..46bcc1c4 100644 --- a/pkg/lexer/smali.go +++ b/pkg/lexer/smali.go @@ -4,8 +4,8 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) @@ -26,42 +26,31 @@ var ( `packed-switch|sparse-switch))\b`) ) -// Smali lexer. -type Smali struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSmali.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Smali) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - var result float32 + lexer.SetAnalyser(func(text string) float32 { + var result float32 - if smaliAnalyserClassRe.MatchString(text) { - result += 0.5 + if smaliAnalyserClassRe.MatchString(text) { + result += 0.5 - if smaliAnalyserClassKeywordsRe.MatchString(text) { - result += 0.3 - } + if smaliAnalyserClassKeywordsRe.MatchString(text) { + result += 0.3 } + } - if smaliAnalyserKeywordsRe.MatchString(text) { - result += 0.6 - } - - return result - }) - - return lexer - } - - return nil -} + if smaliAnalyserKeywordsRe.MatchString(text) { + result += 0.6 + } -// Name returns the name of the lexer. -func (Smali) Name() string { - return heartbeat.LanguageSmali.StringChroma() + return result + }) } diff --git a/pkg/lexer/smali_test.go b/pkg/lexer/smali_test.go old mode 100644 new mode 100755 index d4f1a3bf..ed67df61 --- a/pkg/lexer/smali_test.go +++ b/pkg/lexer/smali_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSmali_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestSmali_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Smali{}.Lexer() + l := lexers.Get(heartbeat.LanguageSmali.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/snowball.go b/pkg/lexer/snowball.go index f78ed8c1..b2f794f8 100644 --- a/pkg/lexer/snowball.go +++ b/pkg/lexer/snowball.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Snowball lexer. Lexer for Snowball source code. -type Snowball struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSnowball.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Snowball) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"snowball"}, Filenames: []string{"*.sbl"}, }, @@ -22,10 +29,5 @@ func (l Snowball) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Snowball) Name() string { - return heartbeat.LanguageSnowball.StringChroma() + )) } diff --git a/pkg/lexer/sourceslist.go b/pkg/lexer/sourceslist.go index b056045a..7ec0955c 100644 --- a/pkg/lexer/sourceslist.go +++ b/pkg/lexer/sourceslist.go @@ -4,20 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var sourcesListAnalyserRe = regexp.MustCompile(`(?m)^\s*(deb|deb-src) `) -// SourcesList lexer. Lexer that highlights debian sources.list files. -type SourcesList struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSourcesList.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l SourcesList) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"sourceslist", "sources.list", "debsources"}, Filenames: []string{"sources.list"}, MimeTypes: []string{"application/x-debian-sourceslist"}, @@ -27,20 +34,11 @@ func (l SourcesList) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if sourcesListAnalyserRe.MatchString(text) { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (SourcesList) Name() string { - return heartbeat.LanguageSourcesList.StringChroma() + })) } diff --git a/pkg/lexer/sourceslist_test.go b/pkg/lexer/sourceslist_test.go old mode 100644 new mode 100755 index ca628d48..0711bda4 --- a/pkg/lexer/sourceslist_test.go +++ b/pkg/lexer/sourceslist_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSourcesList_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestSourcesList_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.SourcesList{}.Lexer() + l := lexers.Get(heartbeat.LanguageSourcesList.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/sqlite3con.go b/pkg/lexer/sqlite3con.go index 9353330b..f405749a 100644 --- a/pkg/lexer/sqlite3con.go +++ b/pkg/lexer/sqlite3con.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Sqlite3con lexer. Lexer for example sessions using sqlite3. -type Sqlite3con struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSqlite3con.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Sqlite3con) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"sqlite3"}, Filenames: []string{"*.sqlite3-console"}, MimeTypes: []string{"text/x-sqlite3-console"}, @@ -23,10 +30,5 @@ func (l Sqlite3con) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Sqlite3con) Name() string { - return heartbeat.LanguageSqlite3con.StringChroma() + )) } diff --git a/pkg/lexer/ssp.go b/pkg/lexer/ssp.go index 12ec5f3e..a0835ce9 100644 --- a/pkg/lexer/ssp.go +++ b/pkg/lexer/ssp.go @@ -5,21 +5,28 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/xml" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var sspAnalyserRe = regexp.MustCompile(`val \w+\s*:`) -// SSP lexer. Lexer for Scalate Server Pages. -type SSP struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSSP.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l SSP) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ssp"}, Filenames: []string{"*.ssp"}, MimeTypes: []string{"application/x-ssp"}, @@ -29,9 +36,7 @@ func (l SSP) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { var result float64 if sspAnalyserRe.MatchString(text) { @@ -47,12 +52,5 @@ func (l SSP) Lexer() chroma.Lexer { } return float32(result) - }) - - return lexer -} - -// Name returns the name of the lexer. -func (SSP) Name() string { - return heartbeat.LanguageSSP.StringChroma() + })) } diff --git a/pkg/lexer/ssp_test.go b/pkg/lexer/ssp_test.go old mode 100644 new mode 100755 index e3bd31b5..7b9ed714 --- a/pkg/lexer/ssp_test.go +++ b/pkg/lexer/ssp_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSSP_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/ssp_basic.ssp") assert.NoError(t, err) - l := lexer.SSP{}.Lexer() + l := lexers.Get(heartbeat.LanguageSSP.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.9), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/stan.go b/pkg/lexer/stan.go index b5a9cb11..dc6607f1 100644 --- a/pkg/lexer/stan.go +++ b/pkg/lexer/stan.go @@ -4,24 +4,27 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var stanAnalyserRe = regexp.MustCompile(`(?m)^\s*parameters\s*\{`) -// Stan lexer. Lexer for Stan models. -// -// The Stan modeling language is specified in the *Stan Modeling Language -// User's Guide and Reference Manual, v2.17.0*, -// pdf `. -type Stan struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageStan.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Stan) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"stan"}, Filenames: []string{"*.stan"}, }, @@ -30,20 +33,11 @@ func (l Stan) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if stanAnalyserRe.MatchString(text) { return 1.0 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Stan) Name() string { - return heartbeat.LanguageStan.StringChroma() + })) } diff --git a/pkg/lexer/stan_test.go b/pkg/lexer/stan_test.go old mode 100644 new mode 100755 index d32b66ea..cb69cf8e --- a/pkg/lexer/stan_test.go +++ b/pkg/lexer/stan_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestStan_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/stan_basic.stan") assert.NoError(t, err) - l := lexer.Stan{}.Lexer() + l := lexers.Get(heartbeat.LanguageStan.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/stata.go b/pkg/lexer/stata.go index b6f0054b..369cfb91 100644 --- a/pkg/lexer/stata.go +++ b/pkg/lexer/stata.go @@ -2,23 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Stata lexer. For Stata do files. -// -// Syntax based on -// - http://fmwww.bc.edu/RePEc/bocode/s/synlightlist.ado -// - https://github.com/isagalaev/highlight.js/blob/master/src/languages/stata.js -// - https://github.com/jpitblado/vim-stata/blob/master/syntax/stata.vim -type Stata struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageStata.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Stata) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"stata", "do"}, Filenames: []string{"*.do", "*.ado"}, MimeTypes: []string{"text/x-stata", "text/stata", "application/x-stata"}, @@ -28,10 +30,5 @@ func (l Stata) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Stata) Name() string { - return heartbeat.LanguageStata.StringChroma() + )) } diff --git a/pkg/lexer/sublimetextconfig.go b/pkg/lexer/sublimetextconfig.go index db7f5d67..ef3d5b7f 100644 --- a/pkg/lexer/sublimetextconfig.go +++ b/pkg/lexer/sublimetextconfig.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// SublimeTextConfig lexer. -type SublimeTextConfig struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSublimeTextConfig.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l SublimeTextConfig) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"sublime"}, Filenames: []string{"*.sublime-settings"}, }, @@ -22,10 +29,5 @@ func (l SublimeTextConfig) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (SublimeTextConfig) Name() string { - return heartbeat.LanguageSublimeTextConfig.StringChroma() + )) } diff --git a/pkg/lexer/supercollider.go b/pkg/lexer/supercollider.go index 6982f077..4cfbaf7f 100644 --- a/pkg/lexer/supercollider.go +++ b/pkg/lexer/supercollider.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// SuperCollider lexer. -type SuperCollider struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSuperCollider.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l SuperCollider) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"sc", "supercollider"}, Filenames: []string{"*.sc", "*.scd"}, MimeTypes: []string{"application/supercollider", "text/supercollider"}, @@ -25,21 +32,12 @@ func (l SuperCollider) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // We're searching for a common function and a unique keyword here. if strings.Contains(text, "SinOsc") || strings.Contains(text, "thisFunctionDef") { return 0.1 } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (SuperCollider) Name() string { - return heartbeat.LanguageSuperCollider.StringChroma() + })) } diff --git a/pkg/lexer/supercollider_test.go b/pkg/lexer/supercollider_test.go old mode 100644 new mode 100755 index 6be2dc7f..4ce012c6 --- a/pkg/lexer/supercollider_test.go +++ b/pkg/lexer/supercollider_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSuperCollider_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestSuperCollider_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.SuperCollider{}.Lexer() + l := lexers.Get(heartbeat.LanguageSuperCollider.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/swig.go b/pkg/lexer/swig.go index b031238c..a45466ac 100644 --- a/pkg/lexer/swig.go +++ b/pkg/lexer/swig.go @@ -4,8 +4,10 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -99,28 +101,30 @@ var ( } ) -// SWIG lexer. -type SWIG struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageSWIG.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l SWIG) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"swig"}, Filenames: []string{"*.swg", "*.i"}, MimeTypes: []string{"text/swig"}, - // Lower than C/C++ and Objective C/C++ - Priority: 0.04, + Priority: 0.04, }, func() chroma.Rules { return chroma.Rules{ "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { var result float32 // Search for SWIG directives, which are conventionally at the beginning of @@ -139,12 +143,5 @@ func (l SWIG) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (SWIG) Name() string { - return heartbeat.LanguageSWIG.StringChroma() + })) } diff --git a/pkg/lexer/swig_test.go b/pkg/lexer/swig_test.go old mode 100644 new mode 100755 index 59684362..7072be6a --- a/pkg/lexer/swig_test.go +++ b/pkg/lexer/swig_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSWIG_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestSWIG_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.SWIG{}.Lexer() + l := lexers.Get(heartbeat.LanguageSWIG.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/tads3.go b/pkg/lexer/tads3.go index a8ae8f12..c85596a6 100644 --- a/pkg/lexer/tads3.go +++ b/pkg/lexer/tads3.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// TADS3 lexer. -type TADS3 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTADS3.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l TADS3) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"tads3"}, Filenames: []string{"*.t"}, }, @@ -24,9 +31,7 @@ func (l TADS3) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // This is a rather generic descriptive language without strong // identifiers. It looks like a 'GameMainDef' has to be present, // and/or a 'versionInfo' with an 'IFID' field. @@ -42,12 +47,5 @@ func (l TADS3) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (TADS3) Name() string { - return heartbeat.LanguageTADS3.StringChroma() + })) } diff --git a/pkg/lexer/tads3_test.go b/pkg/lexer/tads3_test.go old mode 100644 new mode 100755 index fdc4ba58..85b9c393 --- a/pkg/lexer/tads3_test.go +++ b/pkg/lexer/tads3_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTADS3_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestTADS3_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.TADS3{}.Lexer() + l := lexers.Get(heartbeat.LanguageTADS3.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/tap.go b/pkg/lexer/tap.go index 8721a89f..44f45b48 100644 --- a/pkg/lexer/tap.go +++ b/pkg/lexer/tap.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// TAP lexer. For Test Anything Protocol (TAP) output. -type TAP struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTAP.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l TAP) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"tap"}, Filenames: []string{"*.tap"}, }, @@ -22,10 +29,5 @@ func (l TAP) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (TAP) Name() string { - return heartbeat.LanguageTAP.StringChroma() + )) } diff --git a/pkg/lexer/tasm.go b/pkg/lexer/tasm.go index 316b8914..11a9c0ae 100644 --- a/pkg/lexer/tasm.go +++ b/pkg/lexer/tasm.go @@ -4,39 +4,28 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) var tasmAnalyzerRe = regexp.MustCompile(`(?i)PROC`) -// TASM lexer. -type TASM struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTASM.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l TASM) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if tasmAnalyzerRe.MatchString(text) { - return 1.0 - } + lexer.SetAnalyser(func(text string) float32 { + if tasmAnalyzerRe.MatchString(text) { + return 1.0 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (TASM) Name() string { - return heartbeat.LanguageTASM.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/tasm_test.go b/pkg/lexer/tasm_test.go old mode 100644 new mode 100755 index 417bf2e3..86bc36e3 --- a/pkg/lexer/tasm_test.go +++ b/pkg/lexer/tasm_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTASM_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/tasm.asm") assert.NoError(t, err) - l := lexer.TASM{}.Lexer() + l := lexers.Get(heartbeat.LanguageTASM.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(1.0), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/tcshsession.go b/pkg/lexer/tcshsession.go index f043bed1..2e0562cb 100644 --- a/pkg/lexer/tcshsession.go +++ b/pkg/lexer/tcshsession.go @@ -2,19 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// TcshSession lexer. Lexer for Tcsh sessions, i.e. command lines, including a -// prompt, interspersed with output. -type TcshSession struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTcshSession.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l TcshSession) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"tcshcon"}, }, func() chroma.Rules { @@ -22,10 +28,5 @@ func (l TcshSession) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (TcshSession) Name() string { - return heartbeat.LanguageTcshSession.StringChroma() + )) } diff --git a/pkg/lexer/tea.go b/pkg/lexer/tea.go index 5826fd97..696cf9fa 100644 --- a/pkg/lexer/tea.go +++ b/pkg/lexer/tea.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Tea lexer. Lexer for Tea Templates . -type Tea struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTea.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Tea) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"tea"}, Filenames: []string{"*.tea"}, MimeTypes: []string{"text/x-tea"}, @@ -23,10 +30,5 @@ func (l Tea) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Tea) Name() string { - return heartbeat.LanguageTea.StringChroma() + )) } diff --git a/pkg/lexer/teraterm.go b/pkg/lexer/teraterm.go index c6e70144..36c16634 100644 --- a/pkg/lexer/teraterm.go +++ b/pkg/lexer/teraterm.go @@ -5,8 +5,10 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var teraTermAnalyserCommandRe = regexp.MustCompile(`(?i)\b(` + strings.Join([]string{ @@ -211,14 +213,19 @@ var teraTermAnalyserCommandRe = regexp.MustCompile(`(?i)\b(` + strings.Join([]st "zmodemsend", }, "|") + `)\b`) -// TeraTerm macro lexer. -type TeraTerm struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTeraTerm.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l TeraTerm) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ttl", "teraterm", "teratermmacro"}, Filenames: []string{"*.ttl"}, MimeTypes: []string{"text/x-teratermmacro"}, @@ -228,9 +235,7 @@ func (l TeraTerm) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // Turtle and Tera Term macro files share the same file extension // but each has a recognizable and distinct syntax. if teraTermAnalyserCommandRe.MatchString(text) { @@ -238,12 +243,5 @@ func (l TeraTerm) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (TeraTerm) Name() string { - return heartbeat.LanguageTeraTerm.StringChroma() + })) } diff --git a/pkg/lexer/teraterm_test.go b/pkg/lexer/teraterm_test.go old mode 100644 new mode 100755 index f3c30141..2e2348d5 --- a/pkg/lexer/teraterm_test.go +++ b/pkg/lexer/teraterm_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTeraTerm_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/teraterm_commands.ttl") assert.NoError(t, err) - l := lexer.TeraTerm{}.Lexer() + l := lexers.Get(heartbeat.LanguageTeraTerm.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.01), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/tiddler.go b/pkg/lexer/tiddler.go index 3a7aa31a..18183ee2 100644 --- a/pkg/lexer/tiddler.go +++ b/pkg/lexer/tiddler.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Tiddler lexer. For TiddlyWiki5 markup. -type Tiddler struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTiddler.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Tiddler) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"tid"}, Filenames: []string{"*.tid"}, MimeTypes: []string{"text/vnd.tiddlywiki"}, @@ -23,10 +30,5 @@ func (l Tiddler) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Tiddler) Name() string { - return heartbeat.LanguageTiddler.StringChroma() + )) } diff --git a/pkg/lexer/tnt.go b/pkg/lexer/tnt.go index 30562333..3b40a780 100644 --- a/pkg/lexer/tnt.go +++ b/pkg/lexer/tnt.go @@ -2,20 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// TNT lexer. Lexer for Typographic Number Theory, as described in the book -// Gödel, Escher, Bach, by Douglas R. Hofstadter, or as summarized here: -// https://github.com/Kenny2github/language-tnt/blob/master/README.md#summary-of-tnt -type TNT struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTNT.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l TNT) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"tnt"}, Filenames: []string{"*.tnt"}, }, @@ -24,10 +29,5 @@ func (l TNT) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (TNT) Name() string { - return heartbeat.LanguageTNT.StringChroma() + )) } diff --git a/pkg/lexer/todotxt.go b/pkg/lexer/todotxt.go index 9d7a02e4..ee87e2d5 100644 --- a/pkg/lexer/todotxt.go +++ b/pkg/lexer/todotxt.go @@ -2,21 +2,26 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Todotxt lexer. Lexer for Todo.txt todo list format. -type Todotxt struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTodotxt.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Todotxt) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"todotxt"}, - // *.todotxt is not a standard extension for Todo.txt files; including it - // makes testing easier, and also makes autodetecting file type easier. + Name: language, + Aliases: []string{"todotxt"}, Filenames: []string{"todo.txt", "*.todotxt"}, MimeTypes: []string{"text/x-todo"}, }, @@ -25,10 +30,5 @@ func (l Todotxt) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Todotxt) Name() string { - return heartbeat.LanguageTodotxt.StringChroma() + )) } diff --git a/pkg/lexer/trafficscript.go b/pkg/lexer/trafficscript.go index ff438a5f..4cb8c97b 100644 --- a/pkg/lexer/trafficscript.go +++ b/pkg/lexer/trafficscript.go @@ -2,19 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// TrafficScript lexer. For `Riverbed Stingray Traffic Manager -// ` -type TrafficScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTrafficScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l TrafficScript) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"rts", "trafficscript"}, Filenames: []string{"*.rts"}, }, @@ -23,10 +29,5 @@ func (l TrafficScript) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (TrafficScript) Name() string { - return heartbeat.LanguageTrafficScript.StringChroma() + )) } diff --git a/pkg/lexer/transactsql.go b/pkg/lexer/transactsql.go index 415d0a7e..4f8a5ff1 100644 --- a/pkg/lexer/transactsql.go +++ b/pkg/lexer/transactsql.go @@ -4,8 +4,8 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) @@ -17,26 +17,17 @@ var ( tSQLAnalyserNameBetweenBracketRe = regexp.MustCompile(`\[[a-zA-Z_]\w*\]`) ) -// TransactSQL lexer. -type TransactSQL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTransactSQL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l TransactSQL) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - var ( - ok bool - rgxlexer *chroma.RegexLexer - ) - - if rgxlexer, ok = lexer.(*chroma.RegexLexer); !ok { - return nil - } - - rgxlexer.SetAnalyser(func(text string) float32 { + lexer.SetAnalyser(func(text string) float32 { if tSQLAnalyserDeclareRe.MatchString(text) { // Found T-SQL variable declaration. return 1.0 @@ -72,11 +63,4 @@ func (l TransactSQL) Lexer() chroma.Lexer { return result }) - - return rgxlexer -} - -// Name returns the name of the lexer. -func (TransactSQL) Name() string { - return heartbeat.LanguageTransactSQL.StringChroma() } diff --git a/pkg/lexer/transactsql_test.go b/pkg/lexer/transactsql_test.go old mode 100644 new mode 100755 index 0563bb8e..633f3704 --- a/pkg/lexer/transactsql_test.go +++ b/pkg/lexer/transactsql_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTransactSQL_AnalyseText(t *testing.T) { @@ -37,7 +39,8 @@ func TestTransactSQL_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.TransactSQL{}.Lexer() + l := lexers.Get(heartbeat.LanguageTransactSQL.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/treetop.go b/pkg/lexer/treetop.go index fbd6ed8b..8a329ed2 100644 --- a/pkg/lexer/treetop.go +++ b/pkg/lexer/treetop.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Treetop lexer. A lexer for Treetop grammars. -type Treetop struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTreetop.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Treetop) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"treetop"}, Filenames: []string{"*.treetop", "*.tt"}, }, @@ -22,10 +29,5 @@ func (l Treetop) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Treetop) Name() string { - return heartbeat.LanguageTreetop.StringChroma() + )) } diff --git a/pkg/lexer/turtle.go b/pkg/lexer/turtle.go index 3e926bf7..e328c5fa 100644 --- a/pkg/lexer/turtle.go +++ b/pkg/lexer/turtle.go @@ -4,41 +4,30 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) var turtleAnalyserRe = regexp.MustCompile(`^\s*(@base|BASE|@prefix|PREFIX)`) -// Turtle lexer. -type Turtle struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageTurtle.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Turtle) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - // Turtle and Tera Term macro files share the same file extension - // but each has a recognizable and distinct syntax. - if turtleAnalyserRe.MatchString(text) { - return 0.8 - } + lexer.SetAnalyser(func(text string) float32 { + // Turtle and Tera Term macro files share the same file extension + // but each has a recognizable and distinct syntax. + if turtleAnalyserRe.MatchString(text) { + return 0.8 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Turtle) Name() string { - return heartbeat.LanguageTurtle.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/turtle_test.go b/pkg/lexer/turtle_test.go old mode 100644 new mode 100755 index c4693b28..60736095 --- a/pkg/lexer/turtle_test.go +++ b/pkg/lexer/turtle_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTurtle_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/turtle_basic.ttl") assert.NoError(t, err) - l := lexer.Turtle{}.Lexer() + l := lexers.Get(heartbeat.LanguageTurtle.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.8), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/ucode.go b/pkg/lexer/ucode.go index a6bae316..c9316504 100644 --- a/pkg/lexer/ucode.go +++ b/pkg/lexer/ucode.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Ucode lexer. -type Ucode struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageUcode.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Ucode) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"ucode"}, Filenames: []string{"*.u", "*.u1", "*.u2"}, }, @@ -24,9 +31,7 @@ func (l Ucode) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // endsuspend and endrepeat are unique to this language, and // \self, /self doesn't seem to get used anywhere else either. var result float32 @@ -55,12 +60,5 @@ func (l Ucode) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Ucode) Name() string { - return heartbeat.LanguageUcode.StringChroma() + })) } diff --git a/pkg/lexer/ucode_test.go b/pkg/lexer/ucode_test.go old mode 100644 new mode 100755 index 014146db..7b221f86 --- a/pkg/lexer/ucode_test.go +++ b/pkg/lexer/ucode_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestUcode_AnalyseText(t *testing.T) { @@ -41,7 +43,8 @@ func TestUcode_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Ucode{}.Lexer() + l := lexers.Get(heartbeat.LanguageUcode.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/unicon.go b/pkg/lexer/unicon.go index a0ea0634..e8bc2232 100644 --- a/pkg/lexer/unicon.go +++ b/pkg/lexer/unicon.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// Unicon lexer. -type Unicon struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageUnicon.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Unicon) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"unicon"}, Filenames: []string{"*.icn"}, MimeTypes: []string{"text/unicon"}, @@ -23,10 +30,5 @@ func (l Unicon) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (Unicon) Name() string { - return heartbeat.LanguageUnicon.StringChroma() + )) } diff --git a/pkg/lexer/urbiscript.go b/pkg/lexer/urbiscript.go index c4197cf9..8495a83f 100644 --- a/pkg/lexer/urbiscript.go +++ b/pkg/lexer/urbiscript.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// UrbiScript lexer. -type UrbiScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageUrbiScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l UrbiScript) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"urbiscript"}, Filenames: []string{"*.u"}, MimeTypes: []string{"application/x-urbiscript"}, @@ -25,9 +32,7 @@ func (l UrbiScript) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // This is fairly similar to C and others, but freezeif and // waituntil are unique keywords. var result float32 @@ -41,12 +46,5 @@ func (l UrbiScript) Lexer() chroma.Lexer { } return result - }) - - return lexer -} - -// Name returns the name of the lexer. -func (UrbiScript) Name() string { - return heartbeat.LanguageUrbiScript.StringChroma() + })) } diff --git a/pkg/lexer/urbiscript_test.go b/pkg/lexer/urbiscript_test.go old mode 100644 new mode 100755 index b403fbee..925a37a4 --- a/pkg/lexer/urbiscript_test.go +++ b/pkg/lexer/urbiscript_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestUrbiScript_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestUrbiScript_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.UrbiScript{}.Lexer() + l := lexers.Get(heartbeat.LanguageUrbiScript.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/usd.go b/pkg/lexer/usd.go index 74d4f44b..230c8947 100644 --- a/pkg/lexer/usd.go +++ b/pkg/lexer/usd.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// USD lexer. -type USD struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageUSD.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l USD) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"usd", "usda"}, Filenames: []string{"*.usd", "*.usda"}, }, @@ -22,10 +29,5 @@ func (l USD) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (USD) Name() string { - return heartbeat.LanguageUSD.StringChroma() + )) } diff --git a/pkg/lexer/vbnet.go b/pkg/lexer/vbnet.go index 9c3fb602..988e1f0f 100644 --- a/pkg/lexer/vbnet.go +++ b/pkg/lexer/vbnet.go @@ -4,39 +4,28 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) var vbnetAnalyserRe = regexp.MustCompile(`(?m)^\s*(#If|Module|Namespace)`) -// VBNet lexer. -type VBNet struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageVBNet.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l VBNet) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if vbnetAnalyserRe.MatchString(text) { - return 0.5 - } + lexer.SetAnalyser(func(text string) float32 { + if vbnetAnalyserRe.MatchString(text) { + return 0.5 + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (VBNet) Name() string { - return heartbeat.LanguageVBNet.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/vbnet_test.go b/pkg/lexer/vbnet_test.go old mode 100644 new mode 100755 index 65b121e0..6eb967f3 --- a/pkg/lexer/vbnet_test.go +++ b/pkg/lexer/vbnet_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestVBNet_AnalyseText(t *testing.T) { @@ -33,7 +35,8 @@ func TestVBNet_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.VBNet{}.Lexer() + l := lexers.Get(heartbeat.LanguageVBNet.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/vbscript.go b/pkg/lexer/vbscript.go index 345ad9fc..10f3da57 100644 --- a/pkg/lexer/vbscript.go +++ b/pkg/lexer/vbscript.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// VBScript lexer. -type VBScript struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageVBScript.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l VBScript) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"vbscript"}, Filenames: []string{"*.vbs", "*.VBS"}, }, @@ -22,10 +29,5 @@ func (l VBScript) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (VBScript) Name() string { - return heartbeat.LanguageVBScript.StringChroma() + )) } diff --git a/pkg/lexer/vcl.go b/pkg/lexer/vcl.go index 6b064bd7..e684a84b 100644 --- a/pkg/lexer/vcl.go +++ b/pkg/lexer/vcl.go @@ -4,18 +4,25 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// VCL lexer. -type VCL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageVCL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l VCL) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"vcl"}, Filenames: []string{"*.vcl"}, MimeTypes: []string{"text/x-vclsrc"}, @@ -25,9 +32,7 @@ func (l VCL) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { // If the very first line is 'vcl 4.0;' it's pretty much guaranteed // that this is VCL if strings.HasPrefix(text, "vcl 4.0;") { @@ -46,12 +51,5 @@ func (l VCL) Lexer() chroma.Lexer { } return 0 - }) - - return lexer -} - -// Name returns the name of the lexer. -func (VCL) Name() string { - return heartbeat.LanguageVCL.StringChroma() + })) } diff --git a/pkg/lexer/vcl_test.go b/pkg/lexer/vcl_test.go old mode 100644 new mode 100755 index 91a66d89..d1c9bef5 --- a/pkg/lexer/vcl_test.go +++ b/pkg/lexer/vcl_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestVCL_AnalyseText(t *testing.T) { @@ -29,7 +31,8 @@ func TestVCL_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.VCL{}.Lexer() + l := lexers.Get(heartbeat.LanguageVCL.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/vclsnippet.go b/pkg/lexer/vclsnippet.go index c57f7a3e..e9b97123 100644 --- a/pkg/lexer/vclsnippet.go +++ b/pkg/lexer/vclsnippet.go @@ -2,19 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// VCLSnippets lexer. -type VCLSnippets struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageVCLSnippets.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l VCLSnippets) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), - Aliases: []string{"vclsnippets", "vclsnippet"}, + Name: language, + Aliases: []string{"vclsnippets", "vclsnippet"}, + MimeTypes: []string{"text/x-vclsnippet"}, }, func() chroma.Rules { @@ -22,10 +30,5 @@ func (l VCLSnippets) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (VCLSnippets) Name() string { - return heartbeat.LanguageVCLSnippets.StringChroma() + )) } diff --git a/pkg/lexer/vctreestatus.go b/pkg/lexer/vctreestatus.go index 2bc8b80a..a2297997 100644 --- a/pkg/lexer/vctreestatus.go +++ b/pkg/lexer/vctreestatus.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// VCTreeStatus lexer. -type VCTreeStatus struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageVCTreeStatus.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l VCTreeStatus) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"vctreestatus"}, }, func() chroma.Rules { @@ -21,10 +28,5 @@ func (l VCTreeStatus) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (VCTreeStatus) Name() string { - return heartbeat.LanguageVCTreeStatus.StringChroma() + )) } diff --git a/pkg/lexer/velocity.go b/pkg/lexer/velocity.go index c7e4b827..8bdfc62c 100644 --- a/pkg/lexer/velocity.go +++ b/pkg/lexer/velocity.go @@ -4,8 +4,10 @@ import ( "regexp" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) var ( @@ -15,14 +17,19 @@ var ( velocityAnalzserReferenceRe = regexp.MustCompile(`\$!?\{?[a-zA-Z_]\w*(\([^)]*\))?(\.\w+(\([^)]*\))?)*\}?`) ) -// Velocity lexer. -type Velocity struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageVelocity.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Velocity) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"velocity"}, Filenames: []string{"*.vm", "*.fhtml"}, }, @@ -31,9 +38,7 @@ func (l Velocity) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { var result float64 if velocityAnalzserMacroRe.MatchString(text) { @@ -53,12 +58,5 @@ func (l Velocity) Lexer() chroma.Lexer { } return float32(result) - }) - - return lexer -} - -// Name returns the name of the lexer. -func (Velocity) Name() string { - return heartbeat.LanguageVelocity.StringChroma() + })) } diff --git a/pkg/lexer/velocity_test.go b/pkg/lexer/velocity_test.go old mode 100644 new mode 100755 index b6eaead2..89c0a4e8 --- a/pkg/lexer/velocity_test.go +++ b/pkg/lexer/velocity_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestVelocity_AnalyseText(t *testing.T) { @@ -41,7 +43,8 @@ func TestVelocity_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Velocity{}.Lexer() + l := lexers.Get(heartbeat.LanguageVelocity.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/verilog.go b/pkg/lexer/verilog.go index 63daecea..3b629aa8 100644 --- a/pkg/lexer/verilog.go +++ b/pkg/lexer/verilog.go @@ -4,49 +4,38 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// Verilog lexer. -type Verilog struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageVerilog.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l Verilog) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - // Verilog code will use one of reg/wire/assign for sure, and that - // is not common elsewhere. - var result float32 + lexer.SetAnalyser(func(text string) float32 { + // Verilog code will use one of reg/wire/assign for sure, and that + // is not common elsewhere. + var result float32 - if strings.Contains(text, "reg") { - result += 0.1 - } + if strings.Contains(text, "reg") { + result += 0.1 + } - if strings.Contains(text, "wire") { - result += 0.1 - } + if strings.Contains(text, "wire") { + result += 0.1 + } - if strings.Contains(text, "assign") { - result += 0.1 - } + if strings.Contains(text, "assign") { + result += 0.1 + } - return result - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (Verilog) Name() string { - return heartbeat.LanguageVerilog.StringChroma() + return result + }) } diff --git a/pkg/lexer/verilog_test.go b/pkg/lexer/verilog_test.go old mode 100644 new mode 100755 index c175d524..bfafc4d9 --- a/pkg/lexer/verilog_test.go +++ b/pkg/lexer/verilog_test.go @@ -4,9 +4,11 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestVerilog_AnalyseText(t *testing.T) { @@ -37,7 +39,8 @@ func TestVerilog_AnalyseText(t *testing.T) { data, err := os.ReadFile(test.Filepath) assert.NoError(t, err) - l := lexer.Verilog{}.Lexer() + l := lexers.Get(heartbeat.LanguageVerilog.StringChroma()) + require.NotNil(t, l) assert.Equal(t, test.Expected, l.AnalyseText(string(data))) }) diff --git a/pkg/lexer/vgl.go b/pkg/lexer/vgl.go index 17488942..474a967c 100644 --- a/pkg/lexer/vgl.go +++ b/pkg/lexer/vgl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// VGL lexer. -type VGL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageVGL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l VGL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"vgl"}, Filenames: []string{"*.rpf"}, }, @@ -22,10 +29,5 @@ func (l VGL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (VGL) Name() string { - return heartbeat.LanguageVGL.StringChroma() + )) } diff --git a/pkg/lexer/wdiff.go b/pkg/lexer/wdiff.go index bd61f409..c9da14ba 100644 --- a/pkg/lexer/wdiff.go +++ b/pkg/lexer/wdiff.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// WDiff lexer. -type WDiff struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageWDiff.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l WDiff) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"wdiff"}, Filenames: []string{"*.wdiff"}, }, @@ -22,10 +29,5 @@ func (l WDiff) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (WDiff) Name() string { - return heartbeat.LanguageWDiff.StringChroma() + )) } diff --git a/pkg/lexer/webidl.go b/pkg/lexer/webidl.go index f47a66ce..f3b3eb29 100644 --- a/pkg/lexer/webidl.go +++ b/pkg/lexer/webidl.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// WebIDL lexer. -type WebIDL struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageWebIDL.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l WebIDL) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"webidl"}, Filenames: []string{"*.webidl"}, }, @@ -22,10 +29,5 @@ func (l WebIDL) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (WebIDL) Name() string { - return heartbeat.LanguageWebIDL.StringChroma() + )) } diff --git a/pkg/lexer/x10.go b/pkg/lexer/x10.go index 82913d68..3dc808ec 100644 --- a/pkg/lexer/x10.go +++ b/pkg/lexer/x10.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// X10 lexer. -type X10 struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageX10.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l X10) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"x10", "xten"}, Filenames: []string{"*.x10"}, MimeTypes: []string{"text/x-x10"}, @@ -23,10 +30,5 @@ func (l X10) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (X10) Name() string { - return heartbeat.LanguageX10.StringChroma() + )) } diff --git a/pkg/lexer/xaml.go b/pkg/lexer/xaml.go index 928570f1..6f034399 100644 --- a/pkg/lexer/xaml.go +++ b/pkg/lexer/xaml.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// XAML lexer. -type XAML struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageXAML.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l XAML) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"xaml"}, Filenames: []string{"*.xaml"}, MimeTypes: []string{"application/xaml+xml"}, @@ -23,10 +30,5 @@ func (l XAML) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (XAML) Name() string { - return heartbeat.LanguageXAML.StringChroma() + )) } diff --git a/pkg/lexer/xml.go b/pkg/lexer/xml.go index f6e3862b..e53a000e 100644 --- a/pkg/lexer/xml.go +++ b/pkg/lexer/xml.go @@ -2,38 +2,27 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/xml" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" ) -// XML lexer. -type XML struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageXML.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l XML) Lexer() chroma.Lexer { - lexer := lexers.Get(l.Name()) if lexer == nil { - return nil + log.Debugf("lexer %q not found", language) + return } - if lexer, ok := lexer.(*chroma.RegexLexer); ok { - lexer.SetAnalyser(func(text string) float32 { - if xml.MatchString(text) { - return 0.45 // less than HTML. - } + lexer.SetAnalyser(func(text string) float32 { + if xml.MatchString(text) { + return 0.45 // less than HTML. + } - return 0 - }) - - return lexer - } - - return nil -} - -// Name returns the name of the lexer. -func (XML) Name() string { - return heartbeat.LanguageXML.StringChroma() + return 0 + }) } diff --git a/pkg/lexer/xml_test.go b/pkg/lexer/xml_test.go old mode 100644 new mode 100755 index c973ac86..f2b6d1b5 --- a/pkg/lexer/xml_test.go +++ b/pkg/lexer/xml_test.go @@ -4,16 +4,19 @@ import ( "os" "testing" - "github.com/wakatime/wakatime-cli/pkg/lexer" + "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/alecthomas/chroma/v2/lexers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestXML_AnalyseText(t *testing.T) { data, err := os.ReadFile("testdata/xml_doctype_html.xml") assert.NoError(t, err) - l := lexer.XML{}.Lexer() + l := lexers.Get(heartbeat.LanguageXML.StringChroma()) + require.NotNil(t, l) assert.Equal(t, float32(0.45), l.AnalyseText(string(data))) } diff --git a/pkg/lexer/xquery.go b/pkg/lexer/xquery.go index 7bfeaf82..7791e45c 100644 --- a/pkg/lexer/xquery.go +++ b/pkg/lexer/xquery.go @@ -2,18 +2,25 @@ package lexer import ( "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// XQuery lexer. -type XQuery struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageXQuery.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (l XQuery) Lexer() chroma.Lexer { - return chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: l.Name(), + Name: language, Aliases: []string{"xquery", "xqy", "xq", "xql", "xqm"}, Filenames: []string{"*.xqy", "*.xquery", "*.xq", "*.xql", "*.xqm"}, MimeTypes: []string{"text/xquery", "application/xquery"}, @@ -23,10 +30,5 @@ func (l XQuery) Lexer() chroma.Lexer { "root": {}, } }, - ) -} - -// Name returns the name of the lexer. -func (XQuery) Name() string { - return heartbeat.LanguageXQuery.StringChroma() + )) } diff --git a/pkg/lexer/xslt.go b/pkg/lexer/xslt.go index 92ec63c5..d24f013d 100644 --- a/pkg/lexer/xslt.go +++ b/pkg/lexer/xslt.go @@ -4,19 +4,26 @@ import ( "strings" "github.com/wakatime/wakatime-cli/pkg/heartbeat" + "github.com/wakatime/wakatime-cli/pkg/log" "github.com/wakatime/wakatime-cli/pkg/xml" "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/lexers" ) -// XSLT lexer. -type XSLT struct{} +// nolint:gochecknoinits +func init() { + language := heartbeat.LanguageXSLT.StringChroma() + lexer := lexers.Get(language) -// Lexer returns the lexer. -func (XSLT) Lexer() chroma.Lexer { - lexer := chroma.MustNewLexer( + if lexer != nil { + log.Debugf("lexer %q already registered", language) + return + } + + _ = lexers.Register(chroma.MustNewLexer( &chroma.Config{ - Name: "XSLT", + Name: language, Aliases: []string{"xslt"}, // xpl is XProc Filenames: []string{"*.xsl", "*.xslt", "*.xpl"}, @@ -27,20 +34,11 @@ func (XSLT) Lexer() chroma.Lexer { "root": {}, } }, - ) - - lexer.SetAnalyser(func(text string) float32 { + ).SetAnalyser(func(text string) float32 { if xml.MatchString(text) && strings.Contains(text, "