Skip to content

Commit

Permalink
check if feature is activated from ocaml.server
Browse files Browse the repository at this point in the history
remove redundant code

lint

add version

refactor code and lint

refactor code and lint
  • Loading branch information
PizieDust committed Mar 8, 2024
1 parent b6697d5 commit ae226ba
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 49 deletions.
7 changes: 7 additions & 0 deletions ocaml-lsp-server/docs/ocamllsp/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@ interface config {
* @since 1.16
*/
codelens: { enable : boolean }

/**
* Enable/Disable Syntax Documentation
* @default false
* @since 1.18
*/
syntaxDocumentation: { enable : boolean }
}
```
84 changes: 81 additions & 3 deletions ocaml-lsp-server/src/config_data.ml
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ module SyntaxDocumentation = struct
let _ = fun (_ : t) -> ()

let t_of_yojson =
(let _tp_loc = "ocaml-lsp-server/src/config_data.ml.SyntaxDocumentation.t" in
(let _tp_loc =
"ocaml-lsp-server/src/config_data.ml.SyntaxDocumentation.t"
in
function
| `Assoc field_yojsons as yojson -> (
let enable_field = ref Ppx_yojson_conv_lib.Option.None
Expand Down Expand Up @@ -322,7 +324,7 @@ module SyntaxDocumentation = struct
let _ = fun (_ : t) -> ()

let t_of_yojson =
(let _tp_loc = "ocaml-lsp-server/src/config_data.ml.SyntaxDocumentation.t" in
(let _tp_loc ="ocaml-lsp-server/src/config_data.ml.SyntaxDocumentation.t" in
function
| `Assoc field_yojsons as yojson -> (
let enable_field = ref Ppx_yojson_conv_lib.Option.None
Expand Down Expand Up @@ -387,6 +389,80 @@ module SyntaxDocumentation = struct
[@@@end]
end

module SyntaxDocumentation = struct
type t = { enable : bool [@default false] }
[@@deriving_inline yojson] [@@yojson.allow_extra_fields]

let _ = fun (_ : t) -> ()

let t_of_yojson =
(let _tp_loc =
"ocaml-lsp-server/src/config_data.ml.SyntaxDocumentation.t"
in
function
| `Assoc field_yojsons as yojson -> (
let enable_field = ref Ppx_yojson_conv_lib.Option.None
and duplicates = ref []
and extra = ref [] in
let rec iter = function
| (field_name, _field_yojson) :: tail ->
(match field_name with
| "enable" -> (
match Ppx_yojson_conv_lib.( ! ) enable_field with
| Ppx_yojson_conv_lib.Option.None ->
let fvalue = bool_of_yojson _field_yojson in
enable_field := Ppx_yojson_conv_lib.Option.Some fvalue
| Ppx_yojson_conv_lib.Option.Some _ ->
duplicates := field_name :: Ppx_yojson_conv_lib.( ! ) duplicates)
| _ -> ());
iter tail
| [] -> ()
in
iter field_yojsons;
match Ppx_yojson_conv_lib.( ! ) duplicates with
| _ :: _ ->
Ppx_yojson_conv_lib.Yojson_conv_error.record_duplicate_fields
_tp_loc
(Ppx_yojson_conv_lib.( ! ) duplicates)
yojson
| [] -> (
match Ppx_yojson_conv_lib.( ! ) extra with
| _ :: _ ->
Ppx_yojson_conv_lib.Yojson_conv_error.record_extra_fields
_tp_loc
(Ppx_yojson_conv_lib.( ! ) extra)
yojson
| [] ->
let enable_value = Ppx_yojson_conv_lib.( ! ) enable_field in
{ enable =
(match enable_value with
| Ppx_yojson_conv_lib.Option.None -> false
| Ppx_yojson_conv_lib.Option.Some v -> v)
}))
| _ as yojson ->
Ppx_yojson_conv_lib.Yojson_conv_error.record_list_instead_atom
_tp_loc
yojson
: Ppx_yojson_conv_lib.Yojson.Safe.t -> t)

let _ = t_of_yojson

let yojson_of_t =
(function
| { enable = v_enable } ->
let bnds : (string * Ppx_yojson_conv_lib.Yojson.Safe.t) list = [] in
let bnds =
let arg = yojson_of_bool v_enable in
("enable", arg) :: bnds
in
`Assoc bnds
: t -> Ppx_yojson_conv_lib.Yojson.Safe.t)

let _ = yojson_of_t

[@@@end]
end

type t =
{ codelens : Lens.t Json.Nullable_option.t
[@default None] [@yojson_drop_default ( = )]
Expand All @@ -395,7 +471,9 @@ type t =
; inlay_hints : InlayHints.t Json.Nullable_option.t
[@key "inlayHints"] [@default None] [@yojson_drop_default ( = )]
; syntax_documentation : SyntaxDocumentation.t Json.Nullable_option.t
[@key "syntaxDocumentation"] [@default None] [@yojson_drop_default ( = )]
[@key "syntaxDocumentation"]
[@default None]
[@yojson_drop_default ( = )]
}
[@@deriving_inline yojson] [@@yojson.allow_extra_fields]

Expand Down
7 changes: 3 additions & 4 deletions ocaml-lsp-server/src/document.mli
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ module Merlin : sig
val doc_comment :
t -> Msource.position -> (* doc string *) string option Fiber.t

val syntax_doc : Mpipeline.t -> Msource.position -> [ `Found of Query_protocol.syntax_doc_result
| `No_documentation ]
val syntax_doc :
Mpipeline.t -> Msource.position -> Query_protocol.syntax_doc_result option

type type_enclosing =
{ loc : Loc.t
; typ : string
; doc : string option
; syntax_doc_result : [ `Found of Query_protocol.syntax_doc_result
| `No_documentation ]
; syntax_doc : Query_protocol.syntax_doc_result option
}

val type_enclosing :
Expand Down
68 changes: 26 additions & 42 deletions ocaml-lsp-server/src/hover_req.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,33 @@ let format_contents ~syntax ~markdown ~typ ~doc
~(syntax_doc : Query_protocol.syntax_doc_result option) =
(* TODO for vscode, we should just use the language id. But that will not work
for all editors *)
let syn_doc = match Env_vars._ENABLE_SYNTAX_DOCUMENTATION () with
| Some true ->
begin match
syntax_documentation with
| `Found syntax_documentation ->
print_dividers [syntax_documentation.name^": "^syntax_documentation.description;
"Go to [Documentation]("^syntax_documentation.documentation^")"]
| `No_documentation -> ""
end
| Some false | None -> ""
let syntax_doc =
Option.map syntax_doc ~f:(fun syntax_doc ->
sprintf
"_syntax_ %s: %s. See [Manual](%s)"
syntax_doc.name
syntax_doc.description
syntax_doc.documentation)
in
`MarkupContent
(if markdown then
let value =
let markdown_name = Document.Syntax.markdown_name syntax in
let type_info = format_as_code_block [markdown_name; typ] in
match doc with
| None -> sprintf "%s" (print_dividers [type_info; syn_doc])
| Some s ->
`MarkupContent
(if markdown then
let value =
let markdown_name = Document.Syntax.markdown_name syntax in
let type_info = Some (format_as_code_block [ markdown_name; typ ]) in
let doc =
match Doc_to_md.translate s with
| Raw d -> sprintf "(** %s *)" d
| Markdown d -> d
Option.map doc ~f:(fun doc ->
match Doc_to_md.translate doc with
| Raw d -> d
| Markdown d -> d)
in

sprintf
"%s" (print_dividers ([type_info; syn_doc; doc]))
in
{ MarkupContent.value; kind = MarkupKind.Markdown }
else
let value =
match doc with
| None ->
sprintf "%s %s" typ syn_doc
| Some d ->
sprintf "%s\n%s\n%s" typ syn_doc d
in
{ MarkupContent.value; kind = MarkupKind.PlainText })
print_dividers (List.filter_opt [ type_info; syntax_doc; doc ])
in
{ MarkupContent.value; kind = MarkupKind.Markdown }
else
let value =
print_dividers (List.filter_opt [ Some typ; syntax_doc; doc ])
in
{ MarkupContent.value; kind = MarkupKind.PlainText })

let handle server { HoverParams.textDocument = { uri }; position; _ } mode =
Fiber.of_thunk (fun () ->
Expand Down Expand Up @@ -106,12 +95,7 @@ let handle server { HoverParams.textDocument = { uri }; position; _ } mode =
in
match type_enclosing with
| None -> Fiber.return None
| Some
{ Document.Merlin.loc
; typ
; doc = documentation
; syntax_doc_result = syn_doc
} ->
| Some { Document.Merlin.loc; typ; doc = documentation; syntax_doc } ->
let syntax = Document.syntax doc in
let+ typ =
(* We ask Ocamlformat to format this type *)
Expand Down Expand Up @@ -152,7 +136,7 @@ let handle server { HoverParams.textDocument = { uri }; position; _ } mode =
~markdown
~typ
~doc:documentation
~syntax_documentation:syn_doc
~syntax_doc
in
let range = Range.of_loc loc in
Some (Hover.create ~contents ~range ())))

0 comments on commit ae226ba

Please sign in to comment.