Skip to content

Commit d4244cd

Browse files
authored
Merge pull request #36 from gopherds/refactor
import improvements, linting
2 parents 2dfa4d4 + b80e8c9 commit d4244cd

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"
87
"io/ioutil"
@@ -24,36 +23,12 @@ import (
2423
// Importing this package installs Import as go/types.DefaultImport.
2524
"golang.org/x/tools/imports"
2625

27-
"github.com/mitchellh/go-homedir"
2826
"github.com/motemen/go-quickfix"
2927
)
3028

31-
const version = "0.2.5"
32-
const printerName = "__gore_p"
29+
const printerName = "__gophernotes"
3330

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

8560
// printerPkgs is a list of packages that provides
86-
// pretty printing function. Preceding first.
61+
// pretty printing function.
8762
var printerPkgs = []struct {
8863
path string
8964
code string
@@ -95,7 +70,6 @@ var printerPkgs = []struct {
9570

9671
// NewSession initiates a new REPL
9772
func NewSession() (*Session, error) {
98-
var err error
9973

10074
s := &Session{
10175
Fset: token.NewFileSet(),
@@ -104,6 +78,7 @@ func NewSession() (*Session, error) {
10478
},
10579
}
10680

81+
var err error
10782
s.FilePath, err = tempFile()
10883
if err != nil {
10984
return nil, err
@@ -118,12 +93,11 @@ func NewSession() (*Session, error) {
11893
}
11994
debugf("could not import %q: %s", pp.path, err)
12095
}
121-
12296
if initialSource == "" {
123-
return nil, fmt.Errorf(`Could not load pretty printing package (even "fmt"; something is wrong)`)
97+
return nil, fmt.Errorf("Could not load pretty printing package")
12498
}
12599

126-
s.File, err = parser.ParseFile(s.Fset, "gore_session.go", initialSource, parser.Mode(0))
100+
s.File, err = parser.ParseFile(s.Fset, "gophernotes_session.go", initialSource, parser.Mode(0))
127101
if err != nil {
128102
return nil, err
129103
}
@@ -137,22 +111,22 @@ func (s *Session) mainFunc() *ast.FuncDecl {
137111
return s.File.Scope.Lookup("main").Decl.(*ast.FuncDecl)
138112
}
139113

140-
// Run calls "go run" with appropriate files appended
141-
func (s *Session) Run() ([]byte, error, bytes.Buffer) {
114+
// Run calls "go run" with appropriate files appended.
115+
func (s *Session) Run() ([]byte, bytes.Buffer, error) {
142116
f, err := os.Create(s.FilePath)
143117
if err != nil {
144-
return []byte{}, err, bytes.Buffer{}
118+
return nil, bytes.Buffer{}, err
145119
}
146120

147121
err = printer.Fprint(f, s.Fset, s.File)
148122
if err != nil {
149-
return []byte{}, err, bytes.Buffer{}
123+
return nil, bytes.Buffer{}, err
150124
}
151125

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

155-
// tempFile prepares the temporary session file for the REPL
129+
// tempFile prepares the temporary session file for the REPL.
156130
func tempFile() (string, error) {
157131
dir, err := ioutil.TempDir("", "")
158132
if err != nil {
@@ -164,22 +138,20 @@ func tempFile() (string, error) {
164138
return "", err
165139
}
166140

167-
return filepath.Join(dir, "gore_session.go"), nil
141+
return filepath.Join(dir, "gophernotes_session.go"), nil
168142
}
169143

170-
func goRun(files []string) ([]byte, error, bytes.Buffer) {
144+
func goRun(files []string) ([]byte, bytes.Buffer, error) {
171145

172146
var stderr bytes.Buffer
173147

174148
args := append([]string{"run"}, files...)
175149
debugf("go %s", strings.Join(args, " "))
176150
cmd := exec.Command("go", args...)
177151
cmd.Stdin = os.Stdin
178-
//cmd.Stdout = os.Stdout
179-
//cmd.Stderr = os.Stderr
180152
cmd.Stderr = &stderr
181153
out, err := cmd.Output()
182-
return out, err, stderr
154+
return out, stderr, err
183155
}
184156

185157
func (s *Session) evalExpr(in string) (ast.Expr, error) {
@@ -247,10 +219,10 @@ func (s *Session) appendStatements(stmts ...ast.Stmt) {
247219
s.mainBody.List = append(s.mainBody.List, stmts...)
248220
}
249221

250-
// Error is an exported type error
222+
// Error is an exported error.
251223
type Error string
252224

253-
// ErrContinue and ErrQuit are specific exported errors
225+
// ErrContinue and ErrQuit are specific exported error types.
254226
const (
255227
ErrContinue Error = "<continue input>"
256228
ErrQuit Error = "<quit session>"
@@ -286,7 +258,7 @@ func (s *Session) reset() error {
286258
return err
287259
}
288260

289-
file, err := parser.ParseFile(s.Fset, "gore_session.go", source, parser.Mode(0))
261+
file, err := parser.ParseFile(s.Fset, "gophernotes_session.go", source, parser.Mode(0))
290262
if err != nil {
291263
return err
292264
}
@@ -298,34 +270,56 @@ func (s *Session) reset() error {
298270
}
299271

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

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

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

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

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

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

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

@@ -369,18 +363,15 @@ func (s *Session) Eval(in string) (string, error, bytes.Buffer) {
369363
if err = s.importFile(functproxy); err != nil {
370364
errorf("%s", err)
371365
if _, ok := err.(scanner.ErrorList); ok {
372-
return "", ErrContinue, bytes.Buffer{}
366+
return "", bytes.Buffer{}, ErrContinue
373367
}
374368
}
375369
}
376370
}
377371

378-
if *flagAutoImport {
379-
s.fixImports()
380-
}
381372
s.doQuickFix()
382373

383-
output, err, strerr := s.Run()
374+
output, strerr, err := s.Run()
384375
if err != nil {
385376
if exitErr, ok := err.(*exec.ExitError); ok {
386377
// if failed with status 2, remove the last statement
@@ -394,7 +385,7 @@ func (s *Session) Eval(in string) (string, error, bytes.Buffer) {
394385
errorf("%s", err)
395386
}
396387

397-
return string(output), err, strerr
388+
return string(output), strerr, err
398389
}
399390

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

0 commit comments

Comments
 (0)