diff --git a/api/lang/server.go b/api/lang/server.go index c4db665e12..4ab97c7d1b 100644 --- a/api/lang/server.go +++ b/api/lang/server.go @@ -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" ) @@ -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 { diff --git a/cmd/format.go b/cmd/format.go index 242f001bff..3f322def34 100644 --- a/cmd/format.go +++ b/cmd/format.go @@ -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", @@ -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]) }, @@ -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") } diff --git a/config/settings.go b/config/settings.go new file mode 100644 index 0000000000..a483f4c46d --- /dev/null +++ b/config/settings.go @@ -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, ¶ms); 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 +} diff --git a/formatter/formatSpecification.go b/formatter/formatSpecification.go index 8109361ef8..b1e68d0093 100644 --- a/formatter/formatSpecification.go +++ b/formatter/formatSpecification.go @@ -11,6 +11,7 @@ import ( "strings" + "github.com/getgauge/gauge/config" "github.com/getgauge/gauge/gauge" ) @@ -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") } } diff --git a/formatter/formatter.go b/formatter/formatter.go index c0bbec707d..16fb9a6049 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -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 ( @@ -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 { diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index ff886d8fa1..7b348a0ab9 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -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" ) @@ -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"}}, @@ -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 @@ -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} diff --git a/version/version.go b/version/version.go index d69df432e5..880feb762d 100644 --- a/version/version.go +++ b/version/version.go @@ -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 = ""