~fabrixxm/oliver

370c7b21b77ca5c3767c6730b0c08cf8c43daf93 — fabrixxm 6 months ago 1010b07
Search: fix query tokenization
1 files changed, 39 insertions(+), 8 deletions(-)

M src/result.vala
M src/result.vala => src/result.vala +39 -8
@@ 138,12 138,19 @@ namespace Oliver {

            set {
                this._query = value;
                this.tokeinze_and_fold_query ();
                this.changed (DIFFERENT);
            }
        }
        private string[] _query_tokens = {};
        public string[] query_tokens { 
            get {
                return this._query_tokens;
            }
        }


        public override Gtk.FilterMatch get_strictness () {
            debug ("get_strictness");
            if (this.query == "") {
                return Gtk.FilterMatch.ALL;
            } else {


@@ 152,7 159,6 @@ namespace Oliver {
        }

        public override bool match (Object? item) {
            debug ("match");
            if (item == null) {
                return false;
            }


@@ 162,22 168,47 @@ namespace Oliver {

            var obj = (ResultData) item;

            string[] ascii_alternates;
            string[] tokens = this.query.tokenize_and_fold ("en", out ascii_alternates);
            debug ("match query tokens ['%s']", string.joinv ("', '", tokens));

            int score = 0;
            foreach(string token in tokens) {
            foreach(string token in this.query_tokens) {
                var match = obj.file.casefold ().contains (token);
                match = match || obj.message.casefold ().contains (token);
                match = match || obj.line.to_string () == token;
                if (token == "!") {
                    match = match && obj.important;
                    match = match || !obj.ignorable;
                }
                if (match) score++;
            }

            return score == tokens.length;
            return score == this.query_tokens.length;
        }

        void tokeinze_and_fold_query() {
            var query = this.query.casefold ();
            string[] res = {};
            var tok = new StringBuilder ();
            char sep = ' ';
            for(int k = 0; k < query.length; k++) {
                var c = query[k];
                if (c == sep) {
                    if (tok.len != 0) {
                        res += tok.free_and_steal ();
                        tok = new StringBuilder ();
                    }
                    if (sep == '"') sep = ' ';
                } else if (c == '"') {
                    sep = '"';
                } else {
                    tok.append_c (c);
                }

            }
            if (tok.len != 0) {
                res += tok.free_and_steal ();
            }

            debug("query tokens: ['%s']", string.joinv ("', '", res));
            this._query_tokens = res;
        }
    }
}