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

Updates for Goal scripting language, in Code, Numbers, Author. #355

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./... -coverprofile cover.out
run: go test -v ./... -coverprofile cover.out -timeout 30s

- name: Update coverage report
uses: ncruces/go-coverage-report@v0
Expand Down
60 changes: 30 additions & 30 deletions author/book/book.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions author/book/book.cosh → author/book/book.goal
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@ package book

import (
"fmt"

"cogentcore.org/core/base/logx"
"io/fs"
"log/slog"
"os"
"path"
"path/filepath"
"sort"

"cogentcore.org/cogent/author"
"cogentcore.org/cogent/author/refs"
"cogentcore.org/core/base/errors"
"cogentcore.org/core/base/iox/imagex"
"cogentcore.org/core/base/iox/yamlx"
coshell "cogentcore.org/core/shell"
"cogentcore.org/core/base/logx"
goalp "cogentcore.org/lab/goal"
"cogentcore.org/lab/goal/goalib"
)

var (
shell = coshell.NewShell()
goal = goalp.NewGoal()
pdInputs = filepath.Join("author", "pandoc-inputs")
)

Expand Down Expand Up @@ -51,7 +61,7 @@ func Book(c *author.Config) error { //types:add
book.Refs() // note: we allow this to fail, in case using compiled refs
mdfn := book.Markdown()
if logx.UserLevel <= slog.LevelInfo {
shell.Config.Echo = os.Stdout
goal.Config.Echo = os.Stdout
}
var errs []error
for _, fmt := range c.Formats {
Expand Down Expand Up @@ -204,7 +214,7 @@ func (bk *BookData) Markdown() string {
}
cat "endmatter.md" >> {fn}
// todo: appendix
if cosh.FileExists("glossary.md") {
if goalib.FileExists("glossary.md") {
echo "\n<div class=\"book_section\" id=\"glossary\">\n" >> {fn}
cat "glossary.md" >> {fn}
echo "\n</div>" >> {fn}
Expand All @@ -216,10 +226,10 @@ func (bk *BookData) Markdown() string {
}

func (bk *BookData) GetFiles() {
bk.Chapters = cosh.SplitLines(`ls chapter-*.md`)
bk.Chapters = goalib.SplitLines(`ls chapter-*.md`)
sort.Strings(bk.Chapters)
if cosh.FileExists("appendix-a.md") {
bk.Appendicies = cosh.SplitLines(`ls appendix-*.md`)
if goalib.FileExists("appendix-a.md") {
bk.Appendicies = goalib.SplitLines(`ls appendix-*.md`)
sort.Strings(bk.Appendicies)
}
}
Expand All @@ -243,7 +253,7 @@ func (bk *BookData) savePandocInputs() {
fc, _ := fs.ReadFile(PandocInputs, fn)
_, fb := path.Split(fn)
tf := bk.pdi(fb)
cosh.WriteFile(tf, string(fc))
goalib.WriteFile(tf, string(fc))
}
}

Expand Down
2 changes: 1 addition & 1 deletion canvas/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (sv *SVG) Init() {
if e.MouseButton() != events.Left {
return
}
sv.SetFocus()
sv.SetFocusQuiet()
e.SetHandled()
es := sv.EditState()
sob := sv.SelectContainsPoint(e.Pos(), false, true) // not leavesonly, yes exclude existing sels
Expand Down
14 changes: 14 additions & 0 deletions code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ go install cogentcore.org/cogent/code/cmd/cogentcode@latest

* See Releases on this github page for pre-built OS-specific app packages that install the compiled binaries.

## MacOS environment variables

When `Code` is run from an installed mac app bundle, it does not inherit the normal shell environment. To set environment variables in a way that extends to GUI apps, you must use:
```sh
launchctl setenv VAR VALUE
```
The `goal` shell automatically does this for you when setting any environment variables.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the user sets an environment variable in the interactive shell, there is absolutely no way that they expect it to make changes to the underlying system environment. At the very least this should only apply to things in the .goal file, given that I could do set A test or something in the interactive shell while experimenting, and I absolutely would not want that stored in the system forever!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, designing a system that is wholly incompatible with most people's shells is not tenable. Cogent Code must support someone who uses bash and wants to be able to run commands.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just running all commands through separate Goal processes in Cogent Code, which would bypass the environment variable issue entirely since Goal would get the right environment variables on startup? We will be replacing a lot of the current command infrastructure with a Goal terminal interface soon anyway.


This does not extend to the `PATH` variable, which requires this command:
```sh
sudo launchctl config user path $PATH
```
This only takes effect after a reboot. Also, once done, then the default shell path will have that path, already, so commands to set the path in the shell will end up with duplicates.

# Future Plans

We plan to incorporate [gopls](https://github.com/golang/tools/tree/master/gopls) to provide more comprehensive Go language IDE-level support, similar to what is found in VS Code. At present, the completion can be a bit spotty for some kinds of expressions, and the lookup "go to definition" functionality is also not perfect. However, the basic code editing dynamics are pretty solid, so adding gopls would bring it much closer to feature-parity with VS Code.
Expand Down
9 changes: 8 additions & 1 deletion code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,14 @@ func (cv *Code) makeTextEditor(p *tree.Plan, i int) {
})
tree.AddChildAt(w, "texteditor-"+txnm, func(w *TextEditor) {
w.Code = cv
ConfigEditorTextEditor(&w.Editor)
w.Styler(func(s *styles.Style) {
s.Grow.Set(1, 1)
s.Min.X.Ch(20)
s.Min.Y.Em(5)
if w.Buffer != nil {
w.SetReadOnly(w.Buffer.Info.Generated)
}
})
w.OnFocus(func(e events.Event) {
cv.ActiveTextEditorIndex = i
})
Expand Down
10 changes: 10 additions & 0 deletions code/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,16 @@ func (cm *Commands) CopyFrom(cp Commands) {
// MergeAvailableCmds updates the AvailCmds list from CustomCmds and StdCmds
func MergeAvailableCmds() {
AvailableCommands.CopyFrom(StandardCommands)
hasGoal := false
if _, err := exec.LookPath("goal"); err == nil {
hasGoal = true
}
if hasGoal { // replace go build commands with goal build
gob, _, _ := AvailableCommands.CmdByName(CmdName(CommandName("Go", "Build Dir")), false)
gob.Cmds[0].Cmd = "goal"
gob, _, _ = AvailableCommands.CmdByName(CmdName(CommandName("Go", "Build Proj")), false)
gob.Cmds[0].Cmd = "goal"
}
for _, cmd := range CustomCommands {
_, idx, has := AvailableCommands.CmdByName(CmdName(cmd.Label()), false)
if has {
Expand Down
1 change: 1 addition & 0 deletions code/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ func (cv *Code) OpenFileURL(ur string, ftv *texteditor.Editor) bool {
}
}
pos := up.Fragment

tv, _, ok := cv.LinkViewFile(core.Filename(fpath))
if !ok {
_, fnm := filepath.Split(fpath)
Expand Down
24 changes: 23 additions & 1 deletion code/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"cogentcore.org/core/base/fileinfo"
"cogentcore.org/core/base/fsx"
"cogentcore.org/core/core"
"cogentcore.org/core/events"
"cogentcore.org/core/filetree"
Expand Down Expand Up @@ -319,13 +320,34 @@ func (cv *Code) LinkViewFileNode(fn *filetree.Node) (*TextEditor, int) {
return tv, idx
}

// GeneratedFileExts are file extensions for the source file that generates
// another file. If a file is opened that is marked as generated, this list is
// used to look for another file with the same name and the source extention,
// and it is opened instead.
var GeneratedFileExts = map[string]string{
".goal": ".go",
}

// LinkViewFile opens the file in the 2nd texteditor, which is next to
// the tabs where links are clicked, if it is not collapsed -- else 1st
func (cv *Code) LinkViewFile(fnm core.Filename) (*TextEditor, int, bool) {
fn := cv.FileNodeForFile(string(fnm), true)
fn := cv.FileNodeForFile(string(fnm), true) // add if not found
if fn == nil {
return nil, -1, false
}
if fn.Info.Generated {
bfnm, ext := fsx.ExtSplit(string(fnm))
for ex, fex := range GeneratedFileExts {
if fex == ext {
nfnm := bfnm + ex
nfn := cv.FileNodeForFile(nfnm, false)
if nfn != nil {
fn = nfn
break
}
}
}
}
tv, idx, ok := cv.TextEditorForFileNode(fn)
if ok {
if idx == 1 {
Expand Down
4 changes: 2 additions & 2 deletions code/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (on *OpenNodes) DeleteDeleted() {
sz := len(*on)
for i := sz - 1; i >= 0; i-- {
fn := (*on)[i]
if fn.This == nil || fn.FileRoot == nil {
if fn.This == nil || fn.FileRoot() == nil {
on.DeleteIndex(i)
}
}
Expand All @@ -193,7 +193,7 @@ func (on *OpenNodes) Strings() []string {
on.DeleteDeleted()
sl := make([]string, len(*on))
for i, fn := range *on {
rp := fn.FileRoot.RelativePathFrom(fn.Filepath)
rp := fn.FileRoot().RelativePathFrom(fn.Filepath)
rp = strings.TrimSuffix(rp, fn.Name)
if rp != "" {
sl[i] = fn.Name + " - " + rp
Expand Down
11 changes: 0 additions & 11 deletions code/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ type SettingsData struct { //types:add
// file picker settings
Files FileSettings

// environment variables to set for this app -- if run from the command line, standard shell environment variables are inherited, but on some OS's (Mac), they are not set when run as a gui app
EnvVars map[string]string

// if set, the current customized set of language options (see Edit Lang Opts) is saved / loaded along with other settings -- if not set, then you always are using the default compiled-in standard set (which will be updated)
SaveLangOpts bool

Expand Down Expand Up @@ -117,7 +114,6 @@ func (se *SettingsData) Open() error {
func (se *SettingsData) Apply() { //types:add
MergeAvailableCmds()
AvailableLanguages.Validate()
se.ApplyEnvVars()
}

// SetGoMod applies the given gomod setting, setting the GO111MODULE env var
Expand All @@ -129,13 +125,6 @@ func SetGoMod(gomod bool) {
}
}

// ApplyEnvVars applies environment variables set in EnvVars
func (se *SettingsData) ApplyEnvVars() {
for k, v := range se.EnvVars {
os.Setenv(k, v)
}
}

func (se *SettingsData) MakeToolbar(p *tree.Plan) {
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(se.EditLangOpts).SetIcon(icons.Subtitles)
Expand Down
Loading