Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Added logic adding/not adding empty lines when formatting specs based on flag/settings. #2692

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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