Skip to content

Commit

Permalink
Merge pull request #27 from janpfeifer/tracking
Browse files Browse the repository at this point in the history
Tracking of local changes
  • Loading branch information
janpfeifer authored May 23, 2023
2 parents 3f2a2bc + f200bcc commit b372b16
Show file tree
Hide file tree
Showing 10 changed files with 543 additions and 68 deletions.
33 changes: 24 additions & 9 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Package common holds functionality that is common to multiple other packages.
package common

import "sort"
import (
"golang.org/x/exp/constraints"
"sort"
)

// Set implements a Set for the key type T.
type Set[T comparable] map[T]struct{}
Expand All @@ -26,13 +29,25 @@ func (s Set[T]) Insert(key T) {
s[key] = struct{}{}
}

// SortedKeys enumerate keys from a string map and sort them.
// TODO: make it for any key type.
func SortedKeys[T any](m map[string]T) (keys []string) {
keys = make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
// Delete key into set.
func (s Set[T]) Delete(key T) {
delete(s, key)
}

// Keys returns the keys of a map in the form of a slice.
func Keys[K comparable, V any](m map[K]V) []K {
s := make([]K, 0, len(m))
for k := range m {
s = append(s, k)
}
sort.Strings(keys)
return
return s
}

// SortedKeys returns the sorted keys of a map in the form of a slice.
func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K {
s := Keys(m)
sort.Slice(s, func(i, j int) bool {
return s[i] < s[j]
})
return s
}
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# GoNB Changelog

## v0.6.5 - 2023/05/23

* More contextual help and auto-complete improvements:
* Added tracking of files in development (`%track`, `%untrack`), for usage with `gopls`.
* Auto-track `replace` directives in `go.mod` pointing to local filesystem.

## v0.6.4 - 2023/05/22

* More InspectRequest improvements:
Expand Down
138 changes: 86 additions & 52 deletions examples/tutorial.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/janpfeifer/gonb
go 1.20

require (
github.com/fsnotify/fsnotify v1.4.7
github.com/go-language-server/jsonrpc2 v0.4.2
github.com/go-language-server/protocol v0.7.0
github.com/go-language-server/uri v0.2.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/francoispqt/gojay v1.2.13 h1:d2m3sFjloqoIUQU3TsHBgj6qg/BVGlTBeHDUmyJnXKk=
github.com/francoispqt/gojay v1.2.13/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
Expand Down
12 changes: 8 additions & 4 deletions goexec/goexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type State struct {

// gopls client
gopls *goplsclient.Client

// trackingInfo is everything related to tracking.
trackingInfo *trackingInfo
}

// Declarations is a collection of declarations that we carry over from one cell to another.
Expand All @@ -48,10 +51,11 @@ type Declarations struct {
// New returns an empty State object, that can be used to execute Cells.
func New(uniqueID string) (*State, error) {
s := &State{
UniqueID: uniqueID,
Package: "gonb_" + uniqueID,
Decls: NewDeclarations(),
AutoGet: true,
UniqueID: uniqueID,
Package: "gonb_" + uniqueID,
Decls: NewDeclarations(),
AutoGet: true,
trackingInfo: newTrackingInfo(),
}

// Create directory.
Expand Down
19 changes: 16 additions & 3 deletions goexec/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,26 @@ var standardFilesForNotification = []string{
"main.go", "go.mod", "go.sum", "go.work", "other.go",
}

func (s *State) notifyAboutStandardFiles(ctx context.Context) (err error) {
func (s *State) notifyAboutStandardAndTrackedFiles(ctx context.Context) (err error) {
for _, filePath := range standardFilesForNotification {
err = s.gopls.NotifyDidOpenOrChange(ctx, path.Join(s.TempDir, filePath))
if err != nil {
return
}
}

err = s.AutoTrack()
if err != nil {
return
}

err = s.EnumerateUpdatedFiles(func(filePath string) error {
klog.Infof("Notified of change to %q", filePath)
return s.gopls.NotifyDidOpenOrChange(ctx, filePath)
})
if err != nil {
return
}
return
}

Expand Down Expand Up @@ -91,7 +104,7 @@ func (s *State) InspectIdentifierInCell(lines []string, skipLines map[int]struct
s.MainPath(), cursorInFile.Line, cursorInFile.Col)

// Notify about standard files updates:
err = s.notifyAboutStandardFiles(ctx)
err = s.notifyAboutStandardAndTrackedFiles(ctx)
if err != nil {
return
}
Expand Down Expand Up @@ -161,7 +174,7 @@ func (s *State) AutoCompleteOptionsInCell(cellLines []string, skipLines map[int]

// Query `gopls`.
ctx := context.Background()
err = s.notifyAboutStandardFiles(ctx)
err = s.notifyAboutStandardAndTrackedFiles(ctx)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit b372b16

Please sign in to comment.