Skip to content

Commit

Permalink
Merge pull request #32 from rebortg/T6342
Browse files Browse the repository at this point in the history
T6342: add parsing of docs element
  • Loading branch information
dmbaturin authored Jan 14, 2025
2 parents acfac8d + 6148a0e commit c4e441a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/reference_tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ type completion_help_type =
| Script of string [@name "script"]
[@@deriving yojson]

type doc_hints = {
text: string;
hint_type: string;
} [@@deriving yojson]

type docs = {
headline: string;
text: string;
usageExample: string;
hints: doc_hints list;
} [@@deriving yojson]

type ref_node_data = {
node_type: node_type;
constraints: Value_checker.value_constraint list;
Expand All @@ -35,6 +47,7 @@ type ref_node_data = {
default_value: string option;
hidden: bool;
secret: bool;
docs: docs;
} [@@deriving yojson]

type t = ref_node_data Vytree.t [@@deriving yojson]
Expand All @@ -58,6 +71,12 @@ let default_data = {
default_value = None;
hidden = false;
secret = false;
docs = {
headline = "";
text = "";
usageExample = "";
hints = [];
};
}

let default = Vytree.make default_data ""
Expand Down Expand Up @@ -155,6 +174,34 @@ let load_constraint_group_from_xml d c =
| _ -> raise (Bad_interface_definition ("Malformed constraint: " ^ Xml.to_string c))
in Xml.fold aux d c

let load_docs_hints d c =
let aux d c =
match c with
| Xml.Element ("hints", attrs, [Xml.PCData s]) ->
let hint_type = List.assoc "type" attrs in
let hint = { text = s; hint_type = hint_type } in
let new_docs = { d.docs with hints = hint :: d.docs.hints } in
{ d with docs = new_docs }
| _ -> raise (Bad_interface_definition ("Malformed hint: " ^ Xml.to_string c))
in aux d c

let load_docs_from_xml d x =
let aux d x =
match x with
| Xml.Element ("headline", _, [Xml.PCData s]) ->
let new_docs = {d.docs with headline = s} in
{d with docs = new_docs}
| Xml.Element ("text", _, [Xml.PCData s]) ->
let new_docs = {d.docs with text = s} in
{d with docs = new_docs}
| Xml.Element ("hints", _, _) ->
load_docs_hints d x
| Xml.Element ("usageExample", _, [Xml.PCData s]) ->
let new_docs = {d.docs with usageExample = s} in
{d with docs = new_docs}
| _ -> d (* Ignore unknown elements instead of raising an error *)
in Xml.fold aux d x

let data_from_xml d x =
let aux d x =
match x with
Expand All @@ -172,6 +219,7 @@ let data_from_xml d x =
{d with priority=Some i}
| Xml.Element ("hidden", _, _) -> {d with hidden=true}
| Xml.Element ("secret", _, _) -> {d with secret=true}
| Xml.Element ("docs", _, _) -> load_docs_from_xml d x
| _ -> raise (Bad_interface_definition ("Malformed property tag: " ^ Xml.to_string x))
in Xml.fold aux d x

Expand Down
13 changes: 13 additions & 0 deletions src/reference_tree.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ type completion_help_type =
| Script of string [@name "script"]
[@@deriving yojson]

type doc_hints = {
text: string;
hint_type: string;
} [@@deriving yojson]

type docs = {
headline: string;
text: string;
usageExample: string;
hints: doc_hints list;
} [@@deriving to_yojson]

type ref_node_data = {
node_type: node_type;
constraints: Value_checker.value_constraint list;
Expand All @@ -24,6 +36,7 @@ type ref_node_data = {
default_value: string option;
hidden: bool;
secret: bool;
docs: docs;
} [@@deriving yojson]

type t = ref_node_data Vytree.t [@@deriving yojson]
Expand Down

0 comments on commit c4e441a

Please sign in to comment.