Example #1
0
File: io.go Project: wx13/sith
// ReadFile reads in a file (if it exists).
func (file *File) ReadFile(name string) {

	fileInfo, err := os.Stat(name)
	if err != nil {
		file.buffer.ReplaceBuffer(buffer.MakeBuffer([]string{""}))
	} else {
		file.fileMode = fileInfo.Mode()
		stringBuf := []string{""}

		byteBuf, err := ioutil.ReadFile(name)
		if err == nil {
			file.setNewline(string(byteBuf))
			stringBuf = strings.Split(string(byteBuf), file.newline)
		}

		file.buffer.ReplaceBuffer(buffer.MakeBuffer(stringBuf))
	}

	file.ForceSnapshot()
	file.SnapshotSaved()
	file.savedBuffer.ReplaceBuffer(file.buffer.DeepDup())

	file.RequestFlush()

}
Example #2
0
File: file.go Project: wx13/sith
func NewFile(name string, flushChan chan struct{}, screen *terminal.Screen) *File {
	file := &File{
		Name:        name,
		screen:      screen,
		fileMode:    os.FileMode(int(0644)),
		buffer:      buffer.MakeBuffer([]string{""}),
		savedBuffer: buffer.MakeBuffer([]string{""}),
		MultiCursor: cursor.MakeMultiCursor(),
		flushChan:   flushChan,
		saveChan:    make(chan struct{}, 1),
		SyntaxRules: syntaxcolor.NewSyntaxRules(""),
		autoIndent:  true,
		autoTab:     true,
		tabString:   "\t",
		newline:     "\n",
		tabHealth:   true,
		timer:       MakeTimer(),
		maxRate:     100.0,
		statusMutex: &sync.Mutex{},
	}
	file.buffHist = NewBufferHist(file.buffer, file.MultiCursor)
	go file.processSaveRequests()
	go file.ReadFile(name)
	switch path.Ext(name) {
	case ".md", ".txt", ".csv", ".C":
		file.autoIndent = false
	}
	return file
}
Example #3
0
func TestReplaceBuffer(t *testing.T) {
	buf1 := buffer.MakeBuffer([]string{"hello", "world", "", ""})
	buf2 := buffer.MakeBuffer([]string{"yo", "adrian"})
	buf1.ReplaceBuffer(buf2)
	if buf1.GetRow(0).ToString() != "yo" {
		t.Error("ReplaceBuffer failed:", buf1, buf2)
	}
}
Example #4
0
func TestToString(t *testing.T) {
	buf := buffer.MakeBuffer([]string{"hello", "world"})
	str := buf.ToString("\n")
	if str != "hello\nworld" {
		t.Error("ToString is wrong:", str)
	}
}
Example #5
0
func TestRowLength(t *testing.T) {
	buf := buffer.MakeBuffer([]string{"123", "1234"})
	n := buf.RowLength(1)
	if n != 4 {
		t.Error("rowlength is wrong:", n)
	}
}
Example #6
0
func TestBufferAppend(t *testing.T) {
	buf := buffer.MakeBuffer([]string{"", "hello"})
	line := buffer.MakeLine("world")
	buf.Append(line)
	if buf.Length() != 3 {
		t.Error("Buffer Append failed", buf)
	}
}
Example #7
0
func TestMakeBuffer(t *testing.T) {

	var buff buffer.Buffer

	buff = buffer.MakeBuffer([]string{""})
	if buff.Length() != 1 {
		t.Error("MakeBuffer is wrong:", buff)
	}

	buff = buffer.MakeBuffer([]string{"", "", "hello", ""})
	if buff.Length() != 4 {
		t.Error("MakeBuffer is wrong:", buff)
	}
	if buff.GetRow(2).Length() != 5 {
		t.Error("MakeBuffer is wrong:", buff)
	}

}
Example #8
0
func TestInclSlice(t *testing.T) {
	buf := buffer.MakeBuffer([]string{"a", "b", "c", "d", "e"})
	buf2 := buf.InclSlice(1, 2)
	if buf2.Length() != 2 {
		t.Error("InclSlice is wrong:", buf2)
	}
	if buf2.GetRow(1).ToString() != "c" {
		t.Error("InclSlice is wrong:", buf2)
	}
}
Example #9
0
func TestGetIndent(t *testing.T) {
	buf := buffer.MakeBuffer([]string{"  hello", "  world", "    foo"})
	indent, clean := buf.GetIndent()
	if !clean {
		t.Error("should be clean")
	}
	if indent != "  " {
		t.Error("indent should be two spaces")
	}
}
Example #10
0
func TestReplaceLines(t *testing.T) {
	buf := buffer.MakeBuffer([]string{"a", "b", "c", "d", "e"})
	lines := []buffer.Line{
		buffer.MakeLine("hello"),
		buffer.MakeLine("world"),
	}
	buf.ReplaceLines(lines, 1, 3)
	if buf.Length() != 4 {
		t.Error("ReplaceLines failed:", buf.ToString("\n"))
	}
	if buf.GetRow(3).ToString() != "e" {
		t.Error("ReplaceLines failed:", buf.ToString("\n"))
	}
}
Example #11
0
func TestBufferDeepDup(t *testing.T) {

	buf1 := buffer.MakeBuffer([]string{"hello", "world"})
	buf2 := buf1.DeepDup()

	if buf1.GetRow(1).ToString() != buf2.GetRow(1).ToString() {
		t.Error("Duped buffers not equal:", buf1, buf2)
	}

	buf2.SetRow(1, buffer.MakeLine("wurld"))
	if buf1.GetRow(1).ToString() == buf2.GetRow(1).ToString() {
		t.Error("Duped buffers too identical:", buf1, buf2)
	}

}
Example #12
0
File: edit.go Project: wx13/sith
// GoFmt runs the go formatter on the text buffer and updates the buffer.
func (file *File) GoFmt() error {
	filetype := file.SyntaxRules.GetFileType(file.Name)
	if filetype != "go" {
		return errors.New("Will not gofmt a non-go file.")
	}
	contents := file.ToString()
	bytes, err := format.Source([]byte(contents))
	if err == nil {
		stringBuf := strings.Split(string(bytes), file.newline)
		newBuffer := buffer.MakeBuffer(stringBuf)
		file.buffer.ReplaceBuffer(newBuffer)
	}
	file.Snapshot()
	return nil
}
Example #13
0
func TestBufferEdits(t *testing.T) {
	buf := buffer.MakeBuffer([]string{"a", "b", "c"})
	buf.InsertAfter(1, buffer.MakeLine("b2"), buffer.MakeLine("b3"))
	if buf.ToString("-") != "a-b-b2-b3-c" {
		t.Error("InsertAfter is broken", buf.ToString("-"))
	}
	buf.DeleteRow(0)
	if buf.ToString("-") != "b-b2-b3-c" {
		t.Error("InsertAfter is broken", buf.ToString("-"))
	}
	buf.ReplaceLine(buffer.MakeLine("z"), 0)
	if buf.ToString("-") != "z-b2-b3-c" {
		t.Error("InsertAfter is broken", buf.ToString("-"))
	}
	buf.DeleteRow(2)
	if buf.ToString("-") != "z-b2-c" {
		t.Error("InsertAfter is broken", buf.ToString("-"))
	}
}