]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
Added logic to pick lexers based on filename and filetype
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 12 Dec 2017 01:08:10 +0000 (20:08 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 12 Dec 2017 01:08:10 +0000 (20:08 -0500)
Makefile
deps.mk
lib/buf.ml
lib/buf.mli
lib/colormap.ml
lib/colormap.mli
lib/draw.ml
lib/view.ml

index 392a124c9f5ed3d8b0f162afa0a51a9ac6654773..0d98c925aeb6e807cb112dd18871168af9dd5293 100644 (file)
--- 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 d1e54dd0d0a71038d320a367af8913c793f6683b..ebb53f0238c311cb9fa91943d7daaf21bd8bb128 100644 (file)
--- 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
index d40bcfc859078db5d45ba4fb9ef1f1a3aa3840fb..63f864254e0a6591bada2ee7181ee403c916554e 100644 (file)
@@ -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 = {
index 52bb17ba9932874065a545516d01c413b9f44953..a6ddd724edd4f2c3db3596abc80d3d2ac77b4722 100644 (file)
@@ -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
 
index 795a497c88cba662995a83c8a7b191bed5ed02f5..513e0fe54a97b18051c565f31ae9f63531958209 100644 (file)
@@ -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
 
index 80ed134643fa63fba3eba1ba2e3c9248d3edb647..bb2cb8f56d6bc483f388056e23e986d185236ea3 100644 (file)
@@ -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
-*)
index c140ef4e06d23e26be9717af41a059ac47871c12..66c245e0041d8b00d3955e9f569df11dfd2168e5 100644 (file)
@@ -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 = {
index 835f587dbe40762f0ad5789a402447ba5719e33f..f9e20df947cac1311e5940a9ca6ddbbaecccac7b 100644 (file)
@@ -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