Skip to content

Commit

Permalink
org: subtree API for setting and getting property
Browse files Browse the repository at this point in the history
  • Loading branch information
haxscramper committed Mar 19, 2024
1 parent 2749452 commit dfcbefd
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
37 changes: 36 additions & 1 deletion scripts/py_codegen/py_codegen/org_codegen_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,40 @@ def get_types() -> Sequence[GenTuStruct]:
GenTuIdent(t_cr(t_opt(t_str())), "subkind", value="std::nullopt"),
],
),
GenTuFunction(
t("void"),
"removeProperty",
GenTuDoc(
"Remove all instances of the property with matching kind/subkind from the property list"
),
arguments=[
GenTuIdent(t_cr(t_str()), "kind"),
GenTuIdent(t_cr(t_opt(t_str())), "subkind", value="std::nullopt"),
],
),
GenTuFunction(
t("void"),
"setProperty",
GenTuDoc(
"Create or override existing property value in the subtree property list"
),
arguments=[
GenTuIdent(t_cr(t_nest(t("Property"), ["Subtree"])), "value"),
],
),
GenTuFunction(
t("void"),
"setPropertyStrValue",
GenTuDoc(
"Assign a raw string literal to a property.",
"This function will not do the conversion or parsing of the assigned value, so if it is a 'created' or some other property with a typed value, it will still remain as string until the file is written and then parsed back from scratch."
),
arguments=[
GenTuIdent(t_cr(t_str()), "value"),
GenTuIdent(t_cr(t_str()), "kind"),
GenTuIdent(t_cr(t_opt(t_str())), "subkind", value="std::nullopt"),
],
),
],
nested=[
GenTuStruct(
Expand Down Expand Up @@ -1792,7 +1826,8 @@ def get_enums():
GenTuEnumField("CodeCallout",
GenTuDoc("`(refs:` callout in the source code")),
GenTuEnumField("QuoteBlock", GenTuDoc("`#+begin_quote:` block in code")),
GenTuEnumField("CommentBlock", GenTuDoc("`#+begin_comment:` block in code")),
GenTuEnumField("CommentBlock",
GenTuDoc("`#+begin_comment:` block in code")),
GenTuEnumField("AdmonitionBlock", GenTuDoc("")),
GenTuEnumField("CenterBlock", GenTuDoc("'")),
GenTuEnumField("VerseBlock", GenTuDoc("")),
Expand Down
3 changes: 3 additions & 0 deletions scripts/py_haxorg/py_haxorg/pyhaxorg.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ class Subtree(Org):
def getTimePeriods(self, kinds: IntSet[SubtreePeriodKind]) -> List[SubtreePeriod]: ...
def getProperties(self, kind: str, subkind: Optional[str]) -> List[SubtreeProperty]: ...
def getProperty(self, kind: str, subkind: Optional[str]) -> Optional[SubtreeProperty]: ...
def removeProperty(self, kind: str, subkind: Optional[str]) -> None: ...
def setProperty(self, value: SubtreeProperty) -> None: ...
def setPropertyStrValue(self, value: str, kind: str, subkind: Optional[str]) -> None: ...
level: int
treeId: Optional[str]
todo: Optional[str]
Expand Down
15 changes: 15 additions & 0 deletions src/py_libs/pyhaxorg/pyhaxorg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,21 @@ node can have subnodes.)RAW")
static_cast<Opt<sem::Subtree::Property>(sem::Subtree::*)(Str const&, Opt<Str> const&) const>(&sem::Subtree::getProperty),
pybind11::arg("kind"),
pybind11::arg_v("subkind", std::nullopt))
.def("removeProperty",
static_cast<void(sem::Subtree::*)(Str const&, Opt<Str> const&)>(&sem::Subtree::removeProperty),
pybind11::arg("kind"),
pybind11::arg_v("subkind", std::nullopt),
R"RAW(Remove all instances of the property with matching kind/subkind from the property list)RAW")
.def("setProperty",
static_cast<void(sem::Subtree::*)(sem::Subtree::Property const&)>(&sem::Subtree::setProperty),
pybind11::arg("value"),
R"RAW(Create or override existing property value in the subtree property list)RAW")
.def("setPropertyStrValue",
static_cast<void(sem::Subtree::*)(Str const&, Str const&, Opt<Str> const&)>(&sem::Subtree::setPropertyStrValue),
pybind11::arg("value"),
pybind11::arg("kind"),
pybind11::arg_v("subkind", std::nullopt),
R"RAW(Assign a raw string literal to a property.)RAW")
;
pybind11::class_<sem::LatexBody, sem::SemId<sem::LatexBody>, sem::Org>(m, "LatexBody")
;
Expand Down
29 changes: 27 additions & 2 deletions src/sem/SemOrgApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ Org::Org(OrgAdapter original) : original(original), subnodes({}) {}
Org::Org(CVec<SemId<Org>> subnodes) : subnodes(subnodes) {}




Opt<SemId<CmdArgument>> CmdArguments::getParameter(CR<Str> param) const {
return named.get(normalize(param));
}
Expand Down Expand Up @@ -158,6 +156,24 @@ bool HashTag::prefixMatch(CR<Vec<Str>> prefix) const {
}
}

void Subtree::setPropertyStrValue(
Str const& value,
Str const& kind,
CR<Opt<Str>> subkind) {
removeProperty(kind, subkind);
Property::Unknown prop;
prop.name = kind;
auto text = SemId<RawText>::New();
text->text = value;
prop.value = text;
properties.push_back(Property{prop});
}

void Subtree::setProperty(Property const& value) {
removeProperty(value.getName(), value.getSubKind());
properties.push_back(value);
}

Opt<Property> Subtree::getProperty(Str const& kind, CR<Opt<Str>> subkind)
const {
auto props = getProperties(kind, subkind);
Expand All @@ -168,6 +184,15 @@ Opt<Property> Subtree::getProperty(Str const& kind, CR<Opt<Str>> subkind)
}
}

void Subtree::removeProperty(const Str& kind, const Opt<Str>& subkind) {
for (int i = properties.high(); 0 <= i; --i) {
if (properties.at(i).getName() == kind
&& properties.at(i).getSubKind() == subkind) {
properties.erase(properties.begin() + i);
}
}
}

Str Subtree::Property::getName() const {
if (getKind() == Kind::Unknown) {
return getUnknown().name;
Expand Down
13 changes: 12 additions & 1 deletion src/sem/SemOrgTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,10 @@ struct Subtree : public sem::Org {
(OrgSemKind() const) getKind,
(Vec<sem::Subtree::Period>(IntSet<sem::Subtree::Period::Kind>) const) getTimePeriods,
(Vec<sem::Subtree::Property>(Str const&, Opt<Str> const&) const) getProperties,
(Opt<sem::Subtree::Property>(Str const&, Opt<Str> const&) const) getProperty))
(Opt<sem::Subtree::Property>(Str const&, Opt<Str> const&) const) getProperty,
(void(Str const&, Opt<Str> const&)) removeProperty,
(void(sem::Subtree::Property const&)) setProperty,
(void(Str const&, Str const&, Opt<Str> const&)) setPropertyStrValue))
static OrgSemKind const staticKind;
/// \brief Subtree level
int level = 0;
Expand Down Expand Up @@ -1291,6 +1294,14 @@ struct Subtree : public sem::Org {
Vec<sem::Subtree::Period> getTimePeriods(IntSet<sem::Subtree::Period::Kind> kinds) const;
Vec<sem::Subtree::Property> getProperties(Str const& kind, Opt<Str> const& subkind = std::nullopt) const;
Opt<sem::Subtree::Property> getProperty(Str const& kind, Opt<Str> const& subkind = std::nullopt) const;
/// \brief Remove all instances of the property with matching kind/subkind from the property list
void removeProperty(Str const& kind, Opt<Str> const& subkind = std::nullopt);
/// \brief Create or override existing property value in the subtree property list
void setProperty(sem::Subtree::Property const& value);
/// \brief Assign a raw string literal to a property.
///
/// This function will not do the conversion or parsing of the assigned value, so if it is a 'created' or some other property with a typed value, it will still remain as string until the file is written and then parsed back from scratch.
void setPropertyStrValue(Str const& value, Str const& kind, Opt<Str> const& subkind = std::nullopt);
};

/// \brief Latex code body
Expand Down

0 comments on commit dfcbefd

Please sign in to comment.