From: Michael D. Lowis Date: Tue, 12 Dec 2017 01:08:10 +0000 (-0500) Subject: Added logic to pick lexers based on filename and filetype X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=005c0b65e6a54c6f0693187469e6fafad457e24d;p=archive%2Ftide-ocaml.git Added logic to pick lexers based on filename and filetype --- diff --git a/Makefile b/Makefile index 392a124..0d98c92 100644 --- a/Makefile +++ b/Makefile @@ -44,12 +44,12 @@ LIBSRCS = \ lib/misc.ml \ lib/x11.ml \ lib/cfg.ml \ + lib/colormap.ml \ + $(LEXERS) \ lib/rope.ml \ lib/buf.ml \ - lib/colormap.ml \ lib/draw.ml \ lib/scrollmap.ml \ - $(LEXERS) \ lib/view.ml TESTSRCS = \ diff --git a/deps.mk b/deps.mk index d1e54dd..ebb53f0 100644 --- a/deps.mk +++ b/deps.mk @@ -1,8 +1,8 @@ edit.cmo edit.cmi : lib/x11.cmi lib/view.cmi lib/draw.cmi lib/buf.cmi edit.ml edit.cmx edit.o edit.cmi : lib/x11.cmi lib/x11.cmx lib/view.cmi lib/view.cmx lib/draw.cmi lib/draw.cmx lib/buf.cmi lib/buf.cmx edit.ml -lib/buf.cmo : lib/rope.cmi lib/misc.cmi lib/buf.cmi lib/buf.ml -lib/buf.cmx lib/buf.o : lib/rope.cmi lib/rope.cmx lib/misc.cmi lib/misc.cmx lib/buf.cmi lib/buf.ml -lib/buf.cmi : +lib/buf.cmo : lib/rope.cmi lib/misc.cmi lib/colormap.cmi lib/buf.cmi lib/buf.ml +lib/buf.cmx lib/buf.o : lib/rope.cmi lib/rope.cmx lib/misc.cmi lib/misc.cmx lib/colormap.cmi lib/colormap.cmx lib/buf.cmi lib/buf.ml +lib/buf.cmi : lib/colormap.cmi lib/cfg.cmo lib/cfg.cmi : lib/x11.cmi lib/cfg.ml lib/cfg.cmx lib/cfg.o lib/cfg.cmi : lib/x11.cmi lib/x11.cmx lib/cfg.ml lib/colormap.cmo : lib/cfg.cmi lib/colormap.cmi lib/colormap.ml diff --git a/lib/buf.ml b/lib/buf.ml index d40bcfc..63f8642 100644 --- a/lib/buf.ml +++ b/lib/buf.ml @@ -1,4 +1,5 @@ type buf = { + lexfn : Colormap.ctx -> Lexing.lexbuf -> unit; path : string; rope : Rope.t } @@ -10,11 +11,49 @@ type dest = | NextChar | PrevChar | NextLine | PrevLine +type filetype = { + syntax : Colormap.ctx -> Lexing.lexbuf -> unit; + names : string list; + exts : string list; +} + +let filetypes = [ + { + syntax = Lex_cpp.scan; + names = []; + exts = [".c"; ".h"; ".cpp"; ".hpp"; ".cc"; ".c++"; ".cxx"] + }; + { + syntax = Lex_cpp.scan; + names = ["Rakefile"; "rakefile"; "gpkgfile"]; + exts = [".rb"] + }; + { + syntax = Lex_cpp.scan; + names = []; + exts = [".ml"; ".mll"; "mli"] + } +] + +let pick_syntax path = + let name = Filename.basename path in + let ext = Filename.extension path in + let match_ftype ftype = + (List.exists ((=) name) ftype.names) || + (List.exists ((=) ext) ftype.exts) + in match (List.find_opt match_ftype filetypes) with + | Some ft -> ft.syntax + | None -> Lex_cpp.scan + let empty = - { path = ""; rope = Rope.empty } + { lexfn = Lex_cpp.scan; + path = ""; + rope = Rope.empty } let load path = - { path = path; rope = Rope.from_string (Misc.load_file path) } + { lexfn = pick_syntax path; + path = path; + rope = Rope.from_string (Misc.load_file path) } let path buf = buf.path @@ -28,17 +67,19 @@ let iteri fn buf i = let iter fn buf i = iteri (fun i c -> (fn c)) buf i -let make_lexfn buf = +let make_lexer buf = let pos = ref 0 in - (fun bytebuf n -> - let count = ref 0 in - iteri (fun i c -> - Bytes.set bytebuf !count (Char.chr c); - incr count; - (!count >= n) - ) buf !pos; - pos := !pos + !count; - !count) + Colormap.({ + scanfn = buf.lexfn; + lexbuf = Lexing.from_function (fun bytebuf n -> + let count = ref 0 in + iteri (fun i c -> + Bytes.set bytebuf !count (Char.chr c); + incr count; + (!count >= n)) buf !pos; + pos := !pos + !count; + !count) + }) module Cursor = struct type csr = { diff --git a/lib/buf.mli b/lib/buf.mli index 52bb17b..a6ddd72 100644 --- a/lib/buf.mli +++ b/lib/buf.mli @@ -53,5 +53,5 @@ val is_at : dest -> t -> int -> bool val is_bol : t -> int -> bool val is_eol : t -> int -> bool -val make_lexfn : t -> (bytes -> int -> int) +val make_lexer : t -> Colormap.lexer diff --git a/lib/colormap.ml b/lib/colormap.ml index 795a497..513e0fe 100644 --- a/lib/colormap.ml +++ b/lib/colormap.ml @@ -3,11 +3,6 @@ open Lexing exception Eof type style = Normal | Comment | Constant | Keyword | Type | PreProcessor -(* - String | Character | Number | Boolean - | Variable | Function | Keyword | Operator | PreProcessor | Type - | Statement | Special -*) module Span = struct type t = { start : int; stop : int; style : int } @@ -27,15 +22,18 @@ type ctx = { mutable pos : int; } -type lexer = ctx -> Lexing.lexbuf -> unit +type lexer = { + scanfn : ctx -> Lexing.lexbuf -> unit; + lexbuf : Lexing.lexbuf +} let get_color = function -| Normal -> Cfg.Color.Syntax.normal -| Comment -> Cfg.Color.Syntax.comment -| Constant -> Cfg.Color.Syntax.constant -| Keyword -> Cfg.Color.Syntax.keyword -| Type -> Cfg.Color.Syntax.typedef -| PreProcessor -> Cfg.Color.Syntax.preproc + | Normal -> Cfg.Color.Syntax.normal + | Comment -> Cfg.Color.Syntax.comment + | Constant -> Cfg.Color.Syntax.constant + | Keyword -> Cfg.Color.Syntax.keyword + | Type -> Cfg.Color.Syntax.typedef + | PreProcessor -> Cfg.Color.Syntax.preproc let make_span lbuf clr = Span.({ start = (lexeme_start lbuf); @@ -56,10 +54,9 @@ let range_stop ctx clr = in ctx.map <- SpanSet.add span ctx.map -let make scanfn fetchfn = - let lexbuf = Lexing.from_function fetchfn in - let ctx = { lbuf = lexbuf; map = SpanSet.empty; pos = 0; } in - (try while true do scanfn ctx lexbuf done +let make lexer = + let ctx = { lbuf = lexer.lexbuf; map = SpanSet.empty; pos = 0; } in + (try while true do lexer.scanfn ctx lexer.lexbuf done with Eof -> ()); ctx.map diff --git a/lib/colormap.mli b/lib/colormap.mli index 80ed134..bb2cb8f 100644 --- a/lib/colormap.mli +++ b/lib/colormap.mli @@ -8,17 +8,14 @@ type t type ctx -type lexer = ctx -> Lexing.lexbuf -> unit +type lexer = { + scanfn : ctx -> Lexing.lexbuf -> unit; + lexbuf : Lexing.lexbuf +} val empty : t -val make : lexer -> (bytes -> int -> int) -> t +val make : lexer -> t val find : int -> t -> int val set_color : ctx -> style -> unit val range_start : ctx -> unit val range_stop : ctx -> style -> unit - -(* -val from_channel : lexer -> in_channel -> t -val from_string : lexer -> string -> t -val from_function : lexer -> (bytes -> int -> int) -> t -*) diff --git a/lib/draw.ml b/lib/draw.ml index c140ef4..66c245e 100644 --- a/lib/draw.ml +++ b/lib/draw.ml @@ -1,11 +1,10 @@ (* config settings. eventually move to Cfg module *) let font = X11.font_load Cfg.font -let font_height = let open X11 in font.height +let font_height = X11.(font.height) let tabglyph = 0x30 let tabwidth = 4 -let glyph_width g = - let open X11 in g.xoff +let glyph_width g = X11.(g.xoff) module Cursor = struct type t = { diff --git a/lib/view.ml b/lib/view.ml index 835f587..f9e20df 100644 --- a/lib/view.ml +++ b/lib/view.ml @@ -8,7 +8,7 @@ type t = { let from_buffer buf width height = { num = 0; buf = buf; map = Scrollmap.make buf width 0; - clr = Colormap.make Lex_cpp.scan (Buf.make_lexfn buf) } + clr = Colormap.make (Buf.make_lexer buf) } let empty width height = from_buffer (Buf.empty) width height