Esempio n. 1
0
func (s *Session) completeWord(line string, pos int) (string, []string, string) {
	if strings.HasPrefix(line, ":") {
		// complete commands
		if !strings.Contains(line[0:pos], " ") {
			pre, post := line[0:pos], line[pos:]

			result := []string{}
			for _, command := range commands {
				name := ":" + command.name
				if strings.HasPrefix(name, pre) {
					// having complete means that this command takes an argument (for now)
					if !strings.HasPrefix(post, " ") && command.arg != "" {
						name = name + " "
					}
					result = append(result, name)
				}
			}
			return "", result, post
		}

		// complete command arguments
		for _, command := range commands {
			if command.complete == nil {
				continue
			}

			cmdPrefix := ":" + command.name + " "
			if strings.HasPrefix(line, cmdPrefix) && pos >= len(cmdPrefix) {
				return cmdPrefix, command.complete(s, line[len(cmdPrefix):pos]), ""
			}
		}

		return "", nil, ""
	}

	if gocode.Available() == false {
		return "", nil, ""
	}

	// code completion
	pos, cands, err := s.completeCode(line, pos, true)
	if err != nil {
		errorf("completeCode: %s", err)
		return "", nil, ""
	}

	return line[0:pos], cands, ""
}
Esempio n. 2
0
func TestSession_completeCode(t *testing.T) {
	if gocode.Available() == false {
		t.Skipf("gocode unavailable")
	}

	s, err := NewSession()
	noError(t, err)

	err = actionImport(s, "fmt")
	noError(t, err)

	keep, cands, err := s.completeCode("fmt.p", 5, true)
	if err != nil {
		noError(t, err)
	}

	if keep != 4 {
		t.Errorf("keep should be == 4: got %v", keep)
	}

	stringsContain(t, cands, "Println(")
}