Skip to content

Commit b80e8c9

Browse files
committed
import improvements, linting
1 parent e856a16 commit b80e8c9

File tree

3 files changed

+62
-71
lines changed

3 files changed

+62
-71
lines changed

execution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func HandleExecuteRequest(receipt MsgReceipt) {
5757
content["execution_count"] = ExecCounter
5858

5959
// Do the compilation/execution magic.
60-
val, err, stderr := REPLSession.Eval(code)
60+
val, stderr, err := REPLSession.Eval(code)
6161

6262
if err == nil {
6363
content["status"] = "ok"

gophernotes_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestRun_import(t *testing.T) {
3131
}
3232

3333
for _, code := range codes {
34-
_, err, _ := s.Eval(code)
34+
_, _, err := s.Eval(code)
3535
noError(t, err)
3636
}
3737
}
@@ -52,7 +52,7 @@ func TestRun_QuickFix_evaluated_but_not_used(t *testing.T) {
5252
}
5353

5454
for _, code := range codes {
55-
_, err, _ := s.Eval(code)
55+
_, _, err := s.Eval(code)
5656
noError(t, err)
5757
}
5858
}
@@ -70,7 +70,7 @@ func TestRun_QuickFix_used_as_value(t *testing.T) {
7070
}
7171

7272
for _, code := range codes {
73-
_, err, _ := s.Eval(code)
73+
_, _, err := s.Eval(code)
7474
noError(t, err)
7575
}
7676
}
@@ -90,7 +90,7 @@ func TestRun_Copy(t *testing.T) {
9090
}
9191

9292
for _, code := range codes {
93-
_, err, _ := s.Eval(code)
93+
_, _, err := s.Eval(code)
9494
noError(t, err)
9595
}
9696
}
@@ -107,7 +107,7 @@ func TestRun_Const(t *testing.T) {
107107
}
108108

109109
for _, code := range codes {
110-
_, err, _ := s.Eval(code)
110+
_, _, err := s.Eval(code)
111111
noError(t, err)
112112
}
113113
}

internal/repl/repl.go

+56-65
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package replpkg
22

33
import (
44
"bytes"
5-
"flag"
65
"fmt"
76
"io/ioutil"
87
"os"
@@ -23,36 +22,12 @@ import (
2322
// Importing this package installs Import as go/types.DefaultImport.
2423
"golang.org/x/tools/imports"
2524

26-
"github.com/mitchellh/go-homedir"
2725
"github.com/motemen/go-quickfix"
2826
)
2927

30-
const version = "0.2.5"
31-
const printerName = "__gore_p"
28+
const printerName = "__gophernotes"
3229

33-
var (
34-
flagAutoImport = flag.Bool("autoimport", false, "formats and adjusts imports automatically")
35-
flagExtFiles = flag.String("context", "",
36-
"import packages, functions, variables and constants from external golang source files")
37-
flagPkg = flag.String("pkg", "", "specify a package where the session will be run inside")
38-
)
39-
40-
func homeDir() (home string, err error) {
41-
home = os.Getenv("GORE_HOME")
42-
if home != "" {
43-
return
44-
}
45-
46-
home, err = homedir.Dir()
47-
if err != nil {
48-
return
49-
}
50-
51-
home = filepath.Join(home, ".gore")
52-
return
53-
}
54-
55-
// Session encodes info about the current REPL session
30+
// Session encodes info about the current REPL session.
5631
type Session struct {
5732
FilePath string
5833
File *ast.File
@@ -82,7 +57,7 @@ func main() {
8257
`
8358

8459
// printerPkgs is a list of packages that provides
85-
// pretty printing function. Preceding first.
60+
// pretty printing function.
8661
var printerPkgs = []struct {
8762
path string
8863
code string
@@ -94,7 +69,6 @@ var printerPkgs = []struct {
9469

9570
// NewSession initiates a new REPL
9671
func NewSession() (*Session, error) {
97-
var err error
9872

9973
s := &Session{
10074
Fset: token.NewFileSet(),
@@ -103,6 +77,7 @@ func NewSession() (*Session, error) {
10377
},
10478
}
10579

80+
var err error
10681
s.FilePath, err = tempFile()
10782
if err != nil {
10883
return nil, err
@@ -117,12 +92,11 @@ func NewSession() (*Session, error) {
11792
}
11893
debugf("could not import %q: %s", pp.path, err)
11994
}
120-
12195
if initialSource == "" {
122-
return nil, fmt.Errorf(`Could not load pretty printing package (even "fmt"; something is wrong)`)
96+
return nil, fmt.Errorf("Could not load pretty printing package")
12397
}
12498

125-
s.File, err = parser.ParseFile(s.Fset, "gore_session.go", initialSource, parser.Mode(0))
99+
s.File, err = parser.ParseFile(s.Fset, "gophernotes_session.go", initialSource, parser.Mode(0))
126100
if err != nil {
127101
return nil, err
128102
}
@@ -136,22 +110,22 @@ func (s *Session) mainFunc() *ast.FuncDecl {
136110
return s.File.Scope.Lookup("main").Decl.(*ast.FuncDecl)
137111
}
138112

139-
// Run calls "go run" with appropriate files appended
140-
func (s *Session) Run() ([]byte, error, bytes.Buffer) {
113+
// Run calls "go run" with appropriate files appended.
114+
func (s *Session) Run() ([]byte, bytes.Buffer, error) {
141115
f, err := os.Create(s.FilePath)
142116
if err != nil {
143-
return []byte{}, err, bytes.Buffer{}
117+
return nil, bytes.Buffer{}, err
144118
}
145119

146120
err = printer.Fprint(f, s.Fset, s.File)
147121
if err != nil {
148-
return []byte{}, err, bytes.Buffer{}
122+
return nil, bytes.Buffer{}, err
149123
}
150124

151125
return goRun(append(s.ExtraFilePaths, s.FilePath))
152126
}
153127

154-
// tempFile prepares the temporary session file for the REPL
128+
// tempFile prepares the temporary session file for the REPL.
155129
func tempFile() (string, error) {
156130
dir, err := ioutil.TempDir("", "")
157131
if err != nil {
@@ -163,22 +137,20 @@ func tempFile() (string, error) {
163137
return "", err
164138
}
165139

166-
return filepath.Join(dir, "gore_session.go"), nil
140+
return filepath.Join(dir, "gophernotes_session.go"), nil
167141
}
168142

169-
func goRun(files []string) ([]byte, error, bytes.Buffer) {
143+
func goRun(files []string) ([]byte, bytes.Buffer, error) {
170144

171145
var stderr bytes.Buffer
172146

173147
args := append([]string{"run"}, files...)
174148
debugf("go %s", strings.Join(args, " "))
175149
cmd := exec.Command("go", args...)
176150
cmd.Stdin = os.Stdin
177-
//cmd.Stdout = os.Stdout
178-
//cmd.Stderr = os.Stderr
179151
cmd.Stderr = &stderr
180152
out, err := cmd.Output()
181-
return out, err, stderr
153+
return out, stderr, err
182154
}
183155

184156
func (s *Session) evalExpr(in string) (ast.Expr, error) {
@@ -246,10 +218,10 @@ func (s *Session) appendStatements(stmts ...ast.Stmt) {
246218
s.mainBody.List = append(s.mainBody.List, stmts...)
247219
}
248220

249-
// Error is an exported type error
221+
// Error is an exported error.
250222
type Error string
251223

252-
// ErrContinue and ErrQuit are specific exported errors
224+
// ErrContinue and ErrQuit are specific exported error types.
253225
const (
254226
ErrContinue Error = "<continue input>"
255227
ErrQuit Error = "<quit session>"
@@ -285,7 +257,7 @@ func (s *Session) reset() error {
285257
return err
286258
}
287259

288-
file, err := parser.ParseFile(s.Fset, "gore_session.go", source, parser.Mode(0))
260+
file, err := parser.ParseFile(s.Fset, "gophernotes_session.go", source, parser.Mode(0))
289261
if err != nil {
290262
return err
291263
}
@@ -297,34 +269,56 @@ func (s *Session) reset() error {
297269
}
298270

299271
// Eval handles the evaluation of code parsed from received messages
300-
func (s *Session) Eval(in string) (string, error, bytes.Buffer) {
272+
func (s *Session) Eval(in string) (string, bytes.Buffer, error) {
301273
debugf("eval >>> %q", in)
302274

303275
s.clearQuickFix()
304276
s.storeMainBody()
305277

306-
for _, command := range commands {
307-
arg := strings.TrimPrefix(in, ":"+command.name)
308-
if arg == in {
278+
// Split the lines of the input to check for special commands.
279+
inLines := strings.Split(in, "\n")
280+
var nonImportLines []string
281+
for _, line := range inLines {
282+
283+
// Extract non-special lines.
284+
if !strings.HasPrefix(line, "import") && !strings.HasPrefix(line, ":") {
285+
nonImportLines = append(nonImportLines, line)
309286
continue
310287
}
311288

312-
if arg == "" || strings.HasPrefix(arg, " ") {
313-
arg = strings.TrimSpace(arg)
289+
// Process special commands.
290+
for _, command := range commands {
314291

315-
result, err := command.action(s, arg)
316-
if err != nil {
317-
if err == ErrQuit {
318-
return "", err, bytes.Buffer{}
319-
}
320-
errorf("%s: %s", command.name, err)
292+
// Extract any argument provided with the special command.
293+
arg := strings.TrimPrefix(line, ":"+command.name)
294+
if command.name == "import" {
295+
arg = strings.TrimPrefix(arg, "import")
296+
}
297+
if arg == line {
298+
continue
321299
}
322300

323-
s.doQuickFix()
324-
return result, nil, bytes.Buffer{}
301+
// Apply the action associated with the special command.
302+
if arg == "" || strings.HasPrefix(arg, " ") {
303+
arg = strings.TrimSpace(arg)
304+
_, err := command.action(s, arg)
305+
if err != nil {
306+
if err == ErrQuit {
307+
return "", bytes.Buffer{}, err
308+
}
309+
errorf("%s: %s", command.name, err)
310+
}
311+
}
325312
}
326313
}
327314

315+
// Join the non-special lines back together for evaluation.
316+
in = strings.Join(nonImportLines, "\n")
317+
if len(in) == 0 {
318+
s.doQuickFix()
319+
return "", bytes.Buffer{}, nil
320+
}
321+
328322
if _, err := s.evalExpr(in); err != nil {
329323
debugf("expr :: err = %s", err)
330324

@@ -363,18 +357,15 @@ func (s *Session) Eval(in string) (string, error, bytes.Buffer) {
363357
if err = s.importFile(functproxy); err != nil {
364358
errorf("%s", err)
365359
if _, ok := err.(scanner.ErrorList); ok {
366-
return "", ErrContinue, bytes.Buffer{}
360+
return "", bytes.Buffer{}, ErrContinue
367361
}
368362
}
369363
}
370364
}
371365

372-
if *flagAutoImport {
373-
s.fixImports()
374-
}
375366
s.doQuickFix()
376367

377-
output, err, strerr := s.Run()
368+
output, strerr, err := s.Run()
378369
if err != nil {
379370
if exitErr, ok := err.(*exec.ExitError); ok {
380371
// if failed with status 2, remove the last statement
@@ -388,7 +379,7 @@ func (s *Session) Eval(in string) (string, error, bytes.Buffer) {
388379
errorf("%s", err)
389380
}
390381

391-
return string(output), err, strerr
382+
return string(output), strerr, err
392383
}
393384

394385
// storeMainBody stores current state of code so that it can be restored

0 commit comments

Comments
 (0)