Skip to content

Commit

Permalink
config
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Oct 24, 2024
1 parent 9cdcbc8 commit b99ae59
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
24 changes: 24 additions & 0 deletions private/buf/buflsp/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buflsp

// Configuration keys for the LSP.
// Keep in sync with bufbuild/vscode-buf/package.json.
const (
ConfigCLIPath = "buf.commandLine.path"
ConfigCLIUpdates = "buf.commandLine.checkForUpdates"
ConfigBreakingStrategy = "buf.linting.breakingAgainst.strategy"
ConfigBreakingGitRef = "buf.linting.breakingAgainst.gitRef"
)
26 changes: 13 additions & 13 deletions private/buf/buflsp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ const descriptorPath = "google/protobuf/descriptor.proto"

const (
// Compare against the configured git branch.
againstGit againstKind = iota + 1
againstGit againstStrategy = iota + 1
// Against the last saved file on disk (i.e. saved vs unsaved changes).
againstDisk
)

// againstKind is a strategy for selecting which version of a file to use as
// againstStrategy is a strategy for selecting which version of a file to use as
// --against for the purposes of breaking lints.
type againstKind int
type againstStrategy int

// againstKindFromString parses an againstKind from a config setting sent by
// againstFromString parses an againstKind from a config setting sent by
// the client.
//
// Returns againstTrunk, false if the value is not recognized.
func againstKindFromString(s string) (againstKind, bool) {
func againstFromString(s string) (againstStrategy, bool) {
switch s {
// These values are the same as those present in the package.json for the
// VSCode client.
Expand Down Expand Up @@ -90,8 +90,8 @@ type file struct {
workspace bufworkspace.Workspace
module bufmodule.Module

againstKind againstKind
againstGitRef string
againstStrategy againstStrategy
againstGitRef string

objectInfo storage.ObjectInfo
importablePathToObject map[string]storage.ObjectInfo
Expand Down Expand Up @@ -205,19 +205,19 @@ func (f *file) Update(ctx context.Context, version int32, text string) {
func (f *file) RefreshSettings(ctx context.Context) {
settings, err := f.lsp.client.Configuration(ctx, &protocol.ConfigurationParams{
Items: []protocol.ConfigurationItem{
{ScopeURI: f.uri, Section: "buf.against"},
{ScopeURI: f.uri, Section: "buf.againstGit"},
{ScopeURI: f.uri, Section: ConfigBreakingStrategy},
{ScopeURI: f.uri, Section: ConfigBreakingGitRef},
},
})
if err != nil {
// We can throw the error away, since the handler logs it for us.
return
}

f.againstKind = getSetting(f, settings, "buf.against", 0, againstKindFromString)
f.againstGitRef = getSetting(f, settings, "buf.againstGit", 1, func(s string) (string, bool) { return s, true })
f.againstStrategy = getSetting(f, settings, ConfigBreakingStrategy, 0, againstFromString)
f.againstGitRef = getSetting(f, settings, ConfigBreakingGitRef, 1, func(s string) (string, bool) { return s, true })

switch f.againstKind {
switch f.againstStrategy {
case againstDisk:
f.againstGitRef = ""
case againstGit:
Expand Down Expand Up @@ -593,7 +593,7 @@ func (f *file) newAgainstFileOpener(ctx context.Context) fileOpener {
return nil
}

if f.againstKind == againstGit && f.againstGitRef == "" {
if f.againstStrategy == againstGit && f.againstGitRef == "" {
return nil
}

Expand Down

0 comments on commit b99ae59

Please sign in to comment.