Skip to content

Commit

Permalink
Added logic adding/not adding empty lines when formatting specs based…
Browse files Browse the repository at this point in the history
… on flag/settings. (#2692)

* Added logic not adding empty lines when formatting specs, as well as receiving settings from VSC Extension.

Signed-off-by: Jens Johansson <[email protected]>

* Added logic not adding empty lines when formatting specs, as well as receiving settings from VSC Extension.

Signed-off-by: Jens Johansson <[email protected]>

* Added logic not adding empty lines when formatting specs, as well as receiving settings from VSC Extension.

Signed-off-by: Jens Johansson <[email protected]>

---------

Signed-off-by: Jens Johansson <[email protected]>
  • Loading branch information
jensakejohansson authored Feb 3, 2025
1 parent ce9a5a9 commit 706e32a
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 5 deletions.
12 changes: 12 additions & 0 deletions api/lang/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/getgauge/gauge/api/infoGatherer"
"github.com/getgauge/gauge/execution"
"github.com/getgauge/gauge/gauge"
"github.com/getgauge/gauge/config"
"github.com/sourcegraph/jsonrpc2"
)

Expand Down Expand Up @@ -183,6 +184,17 @@ func (h *LangHandler) Handle(ctx context.Context, conn jsonrpc2.JSONRPC2, req *j
logDebug(req, err.Error())
}
return val, err
case "workspace/didChangeConfiguration":
err := config.UpdateSettings(req)
if err != nil {
logDebug(req, err.Error())
e := showErrorMessageOnClient(ctx, conn, err)
if e != nil {
return nil, fmt.Errorf("unable to send '%s' error to LSP server. %s", err.Error(), e.Error())
}
}
logDebug(req, "Updated settings.")
return nil, err
case "gauge/stepReferences":
val, err := stepReferences(req)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/spf13/cobra"
)

var skipEmptyLineInsertions bool

var formatCmd = &cobra.Command{
Use: "format [flags] [args]",
Short: "Formats the specified spec and/or concept files",
Expand All @@ -22,6 +24,7 @@ var formatCmd = &cobra.Command{
exit(err, cmd.UsageString())
}
loadEnvAndReinitLogger(cmd)
config.SetSkipEmptyLineInsertions(skipEmptyLineInsertions)
formatter.FormatSpecFilesIn(getSpecsDir(args)[0])
formatter.FormatConceptFilesIn(getSpecsDir(args)[0])
},
Expand All @@ -30,4 +33,5 @@ var formatCmd = &cobra.Command{

func init() {
GaugeCmd.AddCommand(formatCmd)
formatCmd.Flags().BoolVarP(&skipEmptyLineInsertions, "skip-empty-line-insertions", "s", false, "Skip insertions of empty lines when formatting spec/concept files")
}
46 changes: 46 additions & 0 deletions config/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"encoding/json"

"github.com/sourcegraph/jsonrpc2"
)

var currentSettings GaugeSettings

type FormatConfig struct {
SkipEmptyLineInsertions bool `json:"skipEmptyLineInsertions"`
}

type GaugeSettings struct {
Format FormatConfig `json:"formatting"`
}

type Settings struct {
Gauge GaugeSettings `json:"gauge"`
}

type DidChangeConfigurationParams struct {
Settings Settings `json:"settings"`
}

func UpdateSettings(request *jsonrpc2.Request) error {
var params DidChangeConfigurationParams
if err := json.Unmarshal(*request.Params, &params); err != nil {
return err
}
SetGaugeSettings(params.Settings.Gauge)
return nil
}

func SetGaugeSettings(gs GaugeSettings) {
currentSettings = gs
}

func CurrentGaugeSettings() GaugeSettings {
return currentSettings
}

func SetSkipEmptyLineInsertions(val bool) {
currentSettings.Format.SkipEmptyLineInsertions = val
}
5 changes: 3 additions & 2 deletions formatter/formatSpecification.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"strings"

"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/gauge"
)

Expand All @@ -31,11 +32,11 @@ func (formatter *formatter) Heading(heading *gauge.Heading) {
}

func (formatter *formatter) Tags(tags *gauge.Tags) {
if !strings.HasSuffix(formatter.buffer.String(), "\n\n") {
if !strings.HasSuffix(formatter.buffer.String(), "\n\n") && !config.CurrentGaugeSettings().Format.SkipEmptyLineInsertions {
formatter.buffer.WriteString("\n")
}
formatter.buffer.WriteString(FormatTags(tags))
if formatter.itemQueue.Peek() != nil && (formatter.itemQueue.Peek().Kind() != gauge.CommentKind || strings.TrimSpace(formatter.itemQueue.Peek().(*gauge.Comment).Value) != "") {
if formatter.itemQueue.Peek() != nil && (formatter.itemQueue.Peek().Kind() != gauge.CommentKind || strings.TrimSpace(formatter.itemQueue.Peek().(*gauge.Comment).Value) != "") && !config.CurrentGaugeSettings().Format.SkipEmptyLineInsertions {
formatter.buffer.WriteString("\n")
}
}
Expand Down
3 changes: 3 additions & 0 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/getgauge/gauge/logger"
"github.com/getgauge/gauge/parser"
"github.com/getgauge/gauge/util"
"github.com/getgauge/gauge/config"
)

const (
Expand Down Expand Up @@ -124,7 +125,9 @@ func FormatTable(table *gauge.Table) string {

var tableStringBuffer bytes.Buffer

if !config.CurrentGaugeSettings().Format.SkipEmptyLineInsertions {
tableStringBuffer.WriteString("\n")
}

tableStringBuffer.WriteString(fmt.Sprintf("%s|", getRepeatedChars(" ", tableLeftSpacing)))
for i, header := range table.Headers {
Expand Down
46 changes: 44 additions & 2 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/getgauge/gauge-proto/go/gauge_messages"
"github.com/getgauge/gauge/gauge"
"github.com/getgauge/gauge/parser"
"github.com/getgauge/gauge/config"
. "gopkg.in/check.v1"
)

Expand All @@ -23,12 +24,18 @@ type MySuite struct{}

var _ = Suite(&MySuite{})

// Setup method to make sure each test starts with a known configuration
func (s *MySuite) SetUpTest(c *C) {
config.SetSkipEmptyLineInsertions(false)
}

func (s *MySuite) TestFormatSpecification(c *C) {
tokens := []*parser.Token{
&parser.Token{Kind: gauge.SpecKind, Value: "Spec Heading", LineNo: 1},
&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading", LineNo: 2},
&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 3, Lines: []string{"Example step"}},
&parser.Token{Kind: gauge.StepKind, Value: "Step with inline table", LineNo: 3, Lines: []string{"Step with inline table "}},
&parser.Token{Kind: gauge.TagKind, Args: []string{"test_tag1", "test_tag2"}, LineNo: 3},
&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 4, Lines: []string{"Example step"}},
&parser.Token{Kind: gauge.StepKind, Value: "Step with inline table", LineNo: 5, Lines: []string{"Step with inline table "}},
&parser.Token{Kind: gauge.TableHeader, Args: []string{"id", "name"}},
&parser.Token{Kind: gauge.TableRow, Args: []string{"<1>", "foo"}},
&parser.Token{Kind: gauge.TableRow, Args: []string{"2", "bar"}},
Expand All @@ -41,6 +48,9 @@ func (s *MySuite) TestFormatSpecification(c *C) {
c.Assert(formatted, Equals,
`# Spec Heading
## Scenario Heading
tags: test_tag1, test_tag2
* Example step
* Step with inline table
Expand All @@ -51,6 +61,38 @@ func (s *MySuite) TestFormatSpecification(c *C) {
`)
}

func (s *MySuite) TestFormatSpecificationSkipEmptyLineInsertions(c *C) {
config.SetSkipEmptyLineInsertions(true)
tokens := []*parser.Token{
&parser.Token{Kind: gauge.SpecKind, Value: "Spec Heading", LineNo: 1},
&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading", LineNo: 2},
&parser.Token{Kind: gauge.TagKind, Args: []string{"test_tag1", "test_tag2"}, LineNo: 3},
&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 4, Lines: []string{"Example step"}},
&parser.Token{Kind: gauge.StepKind, Value: "Step with inline table", LineNo: 5, Lines: []string{"Step with inline table "}},
&parser.Token{Kind: gauge.TableHeader, Args: []string{"id", "name"}},
&parser.Token{Kind: gauge.TableRow, Args: []string{"<1>", "foo"}},
&parser.Token{Kind: gauge.TableRow, Args: []string{"2", "bar"}},
}

spec, _, _ := new(parser.SpecParser).CreateSpecification(tokens, gauge.NewConceptDictionary(), "")

formatted := FormatSpecification(spec)

c.Assert(formatted, Equals,
`# Spec Heading
## Scenario Heading
tags: test_tag1, test_tag2
* Example step
* Step with inline table
|id |name|
|---|----|
|<1>|foo |
|2 |bar |
`)
}



func (s *MySuite) TestFormatTable(c *C) {
cell1 := gauge.TableCell{Value: "john", CellType: gauge.Static}
cell2 := gauge.TableCell{Value: "doe", CellType: gauge.Static}
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// CurrentGaugeVersion represents the current version of Gauge
var CurrentGaugeVersion = &Version{1, 6, 12}
var CurrentGaugeVersion = &Version{1, 6, 13}

// BuildMetadata represents build information of current release (e.g, nightly build information)
var BuildMetadata = ""
Expand Down

0 comments on commit 706e32a

Please sign in to comment.