示例#1
0
文件: view.go 项目: rokite/lime
// Will load view settings respect to current syntax
// e.g if current syntax is Python settings order will be:
// Packages/Python/Python.sublime-settings
// Packages/Python/Python (Windows).sublime-settings
// Packages/User/Python.sublime-settings
// <Buffer Specific Settings>
func (v *View) loadSettings() {
	syntax := v.Settings().Get("syntax", "").(string)

	if syntax == "" {
		v.Settings().SetParent(v.window)
		return
	}

	defSettings, usrSettings, platSettings := &HasSettings{}, &HasSettings{}, &HasSettings{}

	defSettings.Settings().SetParent(v.window)
	platSettings.Settings().SetParent(defSettings)
	usrSettings.Settings().SetParent(platSettings)
	v.Settings().SetParent(usrSettings)

	ed := GetEditor()
	if r, err := rubex.Compile(`([A-Za-z]+?)\.(?:[^.]+)$`); err != nil {
		log.Error(err)
		return
	} else if s := r.FindStringSubmatch(syntax); s != nil {
		p := path.Join(LIME_PACKAGES_PATH, s[1], s[1]+".sublime-settings")
		ed.load(packages.NewPacket(p, defSettings.Settings()))

		p = path.Join(LIME_PACKAGES_PATH, s[1], s[1]+" ("+ed.Plat()+").sublime-settings")
		ed.load(packages.NewPacket(p, platSettings.Settings()))

		p = path.Join(LIME_USER_PACKAGES_PATH, s[1]+".sublime-settings")
		ed.load(packages.NewPacket(p, usrSettings.Settings()))
	}
}
示例#2
0
文件: language.go 项目: hanshenu/lime
func (r *Regex) UnmarshalJSON(data []byte) error {
	str := string(data[1 : len(data)-1])
	str = strings.Replace(str, "\\\\", "\\", -1)
	str = strings.Replace(str, "\\n", "\n", -1)
	str = strings.Replace(str, "\\t", "\t", -1)
	if re, err := rubex.Compile(str); err != nil {
		log.Warn("Couldn't compile language pattern %s: %s", str, err)
	} else {
		r.re = re
	}
	return nil
}
示例#3
0
文件: view.go 项目: BinaryPaean/lime
// Classifies point, returning a bitwise OR of zero or more of defined flags
func (v *View) Classify(point int) (res int) {
	var a, b string = "", ""
	ws := v.Settings().Get("word_separators", "[!\"#$%&'()*+,\\-./:;<=>?@\\[\\\\\\]^_`{|}~]").(string)
	if point > 0 {
		a = v.buffer.Substr(Region{point - 1, point})
	}
	if point < v.buffer.Size() {
		b = v.buffer.Substr(Region{point, point + 1})
	}

	// Special cases
	if v.buffer.Size() == 0 || point < 0 || point > v.buffer.Size() {
		res = 3520
		return
	}
	if re, err := rubex.Compile("[A-Z]"); err != nil {
		log4go.Error(err)
	} else {
		if re.MatchString(b) && !re.MatchString(a) {
			res |= CLASS_SUB_WORD_START
			res |= CLASS_SUB_WORD_END
		}
	}
	if a == "," {
		res |= CLASS_OPENING_PARENTHESIS
	}
	if b == "," {
		res |= CLASS_CLOSING_PARENTHESIS
	}
	if a == "," && b == "," {
		res = 0
		return
	}
	// Punc start & end
	if re, err := rubex.Compile(ws); err != nil {
		log4go.Error(err)
	} else {
		if (re.MatchString(b) || b == "") && !re.MatchString(a) {
			res |= CLASS_PUNCTUATION_START
		}
		if (re.MatchString(a) || a == "") && !re.MatchString(b) {
			res |= CLASS_PUNCTUATION_END
		}
		// Word start & end
		if re1, err := rubex.Compile("\\w"); err != nil {
			log4go.Error(err)
		} else if re2, err := rubex.Compile("\\s"); err != nil {
			log4go.Error(err)
		} else {
			if re1.MatchString(b) && (re.MatchString(a) || re2.MatchString(a) || a == "") {
				res |= CLASS_WORD_START
			}
			if re1.MatchString(a) && (re.MatchString(b) || re2.MatchString(b) || b == "") {
				res |= CLASS_WORD_END
			}
		}
	}
	// SubWord start & end

	// Line start & end
	if a == "\n" || a == "" {
		res |= CLASS_LINE_START
	}
	if b == "\n" || b == "" {
		res |= CLASS_LINE_END
	}
	// Empty line
	if (a == "\n" && b == "\n") || (a == "" && b == "") {
		res |= CLASS_EMPTY_LINE
	}
	// Middle word
	if re, err := rubex.Compile("\\w"); err != nil {
		log4go.Error(err)
	} else {
		if re.MatchString(a) && re.MatchString(b) {
			res |= CLASS_MIDDLE_WORD
		}
	}
	// Word start & end with punc
	if re, err := rubex.Compile("\\s"); err != nil {
		log4go.Error(err)
	} else {
		if (res&CLASS_PUNCTUATION_START == CLASS_PUNCTUATION_START) && (re.MatchString(a) || a == "") {
			res |= CLASS_WORD_START_WITH_PUNCTUATION
		}
		if (res&CLASS_PUNCTUATION_END == CLASS_PUNCTUATION_END) && (re.MatchString(b) || b == "") {
			res |= CLASS_WORD_END_WITH_PUNCTUATION
		}
	}
	// Openning & closing parentheses
	if re, err := rubex.Compile("[(\\[{]"); err != nil {
		log4go.Error(err)
	} else {
		if re.MatchString(a) || re.MatchString(b) {
			res |= CLASS_OPENING_PARENTHESIS
		}
		if re.MatchString(a) && a == b {
			res = 0
			return
		}
	}
	if re, err := rubex.Compile("[)\\]}]"); err != nil {
		log4go.Error(err)
	} else {
		if re.MatchString(a) || re.MatchString(b) {
			res |= CLASS_CLOSING_PARENTHESIS
		}
		if re.MatchString(a) && a == b {
			res = 0
			return
		}
	}
	return
}
示例#4
0
// Classifies point, returning a bitwise OR of zero or more of defined flags
func (v *View) Classify(point int) (res int) {
	var a, b string = "", ""
	ws := v.Settings().Get("word_separators", DEFAULT_SEPARATORS).(string)
	if point > 0 {
		a = v.buffer.Substr(Region{point - 1, point})
	}
	if point < v.buffer.Size() {
		b = v.buffer.Substr(Region{point, point + 1})
	}

	// Out of range
	if v.buffer.Size() == 0 || point < 0 || point > v.buffer.Size() {
		res = 3520
		return
	}

	// If before and after the point are separators return 0
	if re, err := rubex.Compile(ws); err != nil {
		log.Error(err)
	} else if a == b && re.MatchString(a) {
		res = 0
		return
	}

	// SubWord start & end
	if re, err := rubex.Compile("[A-Z]"); err != nil {
		log.Error(err)
	} else {
		if re.MatchString(b) && !re.MatchString(a) {
			res |= CLASS_SUB_WORD_START
			res |= CLASS_SUB_WORD_END
		}
	}
	if a == "_" && b != "_" {
		res |= CLASS_SUB_WORD_START
	}
	if b == "_" && a != "_" {
		res |= CLASS_SUB_WORD_END
	}

	// Punc start & end
	if re, err := rubex.Compile(ws); err != nil {
		log.Error(err)
	} else {
		// Why ws != ""? See https://github.com/limetext/rubex/issues/2
		if ((re.MatchString(b) && ws != "") || b == "") && !(re.MatchString(a) && ws != "") {
			res |= CLASS_PUNCTUATION_START
		}
		if ((re.MatchString(a) && ws != "") || a == "") && !(re.MatchString(b) && ws != "") {
			res |= CLASS_PUNCTUATION_END
		}
		// Word start & end
		if re1, err := rubex.Compile("\\w"); err != nil {
			log.Error(err)
		} else if re2, err := rubex.Compile("\\s"); err != nil {
			log.Error(err)
		} else {
			if re1.MatchString(b) && ((re.MatchString(a) && ws != "") || re2.MatchString(a) || a == "") {
				res |= CLASS_WORD_START
			}
			if re1.MatchString(a) && ((re.MatchString(b) && ws != "") || re2.MatchString(b) || b == "") {
				res |= CLASS_WORD_END
			}
		}
	}

	// Line start & end
	if a == "\n" || a == "" {
		res |= CLASS_LINE_START
	}
	if b == "\n" || b == "" {
		res |= CLASS_LINE_END
		if ws == "" {
			res |= CLASS_WORD_END
		}
	}

	// Empty line
	if (a == "\n" && b == "\n") || (a == "" && b == "") {
		res |= CLASS_EMPTY_LINE
	}
	// Middle word
	if re, err := rubex.Compile("\\w"); err != nil {
		log.Error(err)
	} else {
		if re.MatchString(a) && re.MatchString(b) {
			res |= CLASS_MIDDLE_WORD
		}
	}

	// Word start & end with punc
	if re, err := rubex.Compile("\\s"); err != nil {
		log.Error(err)
	} else {
		if (res&CLASS_PUNCTUATION_START != 0) && (re.MatchString(a) || a == "") {
			res |= CLASS_WORD_START_WITH_PUNCTUATION
		}
		if (res&CLASS_PUNCTUATION_END != 0) && (re.MatchString(b) || b == "") {
			res |= CLASS_WORD_END_WITH_PUNCTUATION
		}
	}

	// Openning & closing parentheses
	if re, err := rubex.Compile("[(\\[{]"); err != nil {
		log.Error(err)
	} else {
		if re.MatchString(a) || re.MatchString(b) {
			res |= CLASS_OPENING_PARENTHESIS
		}
	}
	if re, err := rubex.Compile("[)\\]}]"); err != nil {
		log.Error(err)
	} else {
		if re.MatchString(a) || re.MatchString(b) {
			res |= CLASS_CLOSING_PARENTHESIS
		}
	}
	// TODO: isn't this a bug? what's the relation between
	// ',' and parentheses
	if a == "," {
		res |= CLASS_OPENING_PARENTHESIS
	}
	if b == "," {
		res |= CLASS_CLOSING_PARENTHESIS
	}

	return
}