use jsonx_lib::Value;
use crate::{
json::{req_object, req_str, to_value, write_str},
position::Range,
response::ResponseError,
};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Location {
pub uri: String,
pub range: Range,
}
impl Location {
#[must_use]
pub fn new(uri: impl Into<String>, range: Range) -> Self {
Self {
uri: uri.into(),
range,
}
}
#[must_use]
pub fn to_json(&self) -> Value<'static> {
let mut out = String::new();
self.write(&mut out);
to_value(&out)
}
pub fn from_json(value: &Value<'_>) -> Result<Self, ResponseError> {
Ok(Self {
uri: req_str(value, "uri")?.to_owned(),
range: Range::from_json(req_object(value, "range")?)?,
})
}
pub(crate) fn write(&self, out: &mut String) {
out.push_str("{\"uri\":");
write_str(out, &self.uri);
out.push_str(",\"range\":");
self.range.write(out);
out.push('}');
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MarkupKind {
PlainText,
#[default]
Markdown,
}
impl MarkupKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
MarkupKind::PlainText => "plaintext",
MarkupKind::Markdown => "markdown",
}
}
}
pub(crate) fn write_markup(out: &mut String, kind: MarkupKind, value: &str) {
out.push_str("{\"kind\":\"");
out.push_str(kind.as_str());
out.push_str("\",\"value\":");
write_str(out, value);
out.push('}');
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Hover {
pub contents: String,
pub kind: MarkupKind,
pub range: Option<Range>,
}
impl Hover {
#[must_use]
pub fn markdown(contents: impl Into<String>) -> Self {
Self {
contents: contents.into(),
kind: MarkupKind::Markdown,
range: None,
}
}
#[must_use]
pub fn plain_text(contents: impl Into<String>) -> Self {
Self {
contents: contents.into(),
kind: MarkupKind::PlainText,
range: None,
}
}
#[must_use]
pub fn with_range(mut self, range: Range) -> Self {
self.range = Some(range);
self
}
#[must_use]
pub fn to_json(&self) -> Value<'static> {
let mut out = String::new();
self.write(&mut out);
to_value(&out)
}
pub(crate) fn write(&self, out: &mut String) {
out.push_str("{\"contents\":");
write_markup(out, self.kind, &self.contents);
if let Some(range) = &self.range {
out.push_str(",\"range\":");
range.write(out);
}
out.push('}');
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TextEdit {
pub range: Range,
pub new_text: String,
}
impl TextEdit {
#[must_use]
pub fn new(range: Range, new_text: impl Into<String>) -> Self {
Self {
range,
new_text: new_text.into(),
}
}
#[must_use]
pub fn to_json(&self) -> Value<'static> {
let mut out = String::new();
self.write(&mut out);
to_value(&out)
}
pub fn from_json(value: &Value<'_>) -> Result<Self, ResponseError> {
Ok(Self {
range: Range::from_json(req_object(value, "range")?)?,
new_text: req_str(value, "newText")?.to_owned(),
})
}
pub(crate) fn write(&self, out: &mut String) {
out.push_str("{\"range\":");
self.range.write(out);
out.push_str(",\"newText\":");
write_str(out, &self.new_text);
out.push('}');
}
}
#[cfg(test)]
mod tests {
use jsonx_lib::json;
use super::*;
use crate::position::Position;
fn range() -> Range {
Range::new(Position::new(1, 2), Position::new(3, 4))
}
#[test]
fn test_location_round_trips() {
let at = Location::new("file:///\"q\"", range());
assert_eq!(Location::from_json(&at.to_json()).ok(), Some(at));
assert!(Location::from_json(&json!({ "uri": "x" })).is_err());
assert!(Location::from_json(&json!({ "uri": 3, "range": {} })).is_err());
}
#[test]
fn test_hover_json_shapes() {
let plain = Hover::plain_text("t\n\"x\"").to_json();
assert_eq!(plain["contents"]["kind"], "plaintext");
assert_eq!(plain["contents"]["value"], "t\n\"x\"");
assert!(plain.get("range").is_none());
let ranged = Hover::markdown("m").with_range(range()).to_json();
assert_eq!(ranged["range"]["end"]["line"], 3);
}
#[test]
fn test_text_edit_round_trips_and_rejects_bad_shapes() {
let edit = TextEdit::new(range(), "é\n");
assert_eq!(TextEdit::from_json(&edit.to_json()).ok(), Some(edit));
assert!(TextEdit::from_json(&json!({ "range": {}, "newText": "" })).is_err());
assert!(TextEdit::from_json(&json!({ "newText": "" })).is_err());
}
}