Skip to content

Commit

Permalink
Add completions and hover (facebook#79)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#79

This is code from facebook#68, with cleanups.

Differential Revision: D47208501

fbshipit-source-id: 7f06543338246494054e739c6fc2066bbe31941f
  • Loading branch information
ndmitchell authored and facebook-github-bot committed Jul 5, 2023
1 parent 07d1e65 commit 7c7d170
Show file tree
Hide file tree
Showing 11 changed files with 1,804 additions and 171 deletions.
17 changes: 15 additions & 2 deletions starlark/src/docs/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

use std::slice;

use dupe::Dupe;
use itertools::Itertools;
use starlark_map::small_map::SmallMap;
Expand Down Expand Up @@ -238,15 +240,26 @@ fn render_object(name: &str, object: &DocObject) -> String {
render_members(name, true, &object.docs, &object.members)
}

fn render_doc_item(name: &str, item: &DocItem) -> String {
match &item {
pub(crate) fn render_doc_item(name: &str, item: &DocItem) -> String {
match item {
DocItem::Module(m) => render_module(name, m),
DocItem::Object(o) => render_object(name, o),
DocItem::Function(f) => render_function(name, f),
DocItem::Property(p) => render_property(name, p),
}
}

pub(crate) fn render_doc_member(name: &str, item: &DocMember) -> String {
match item {
DocMember::Function(f) => render_function(name, f),
DocMember::Property(p) => render_property(name, p),
}
}

pub(crate) fn render_doc_param(item: &DocParam) -> String {
render_function_parameters(slice::from_ref(item)).unwrap_or_default()
}

impl RenderMarkdown for Doc {
fn render_markdown_opt(&self, flavor: MarkdownFlavor) -> Option<String> {
match flavor {
Expand Down
72 changes: 67 additions & 5 deletions starlark/src/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// TODO(nga): document it
#![allow(missing_docs)]

mod markdown;
pub(crate) mod markdown;

use std::collections::HashMap;

Expand Down Expand Up @@ -349,7 +349,7 @@ pub struct DocModule {
}

impl DocModule {
fn render_as_code(&self) -> String {
pub(crate) fn render_as_code(&self) -> String {
let mut res = self
.docs
.as_ref()
Expand Down Expand Up @@ -425,7 +425,7 @@ impl DocFunction {
}
}

fn render_as_code(&self, name: &str) -> String {
pub(crate) fn render_as_code(&self, name: &str) -> String {
let params: Vec<_> = self.params.iter().map(DocParam::render_as_code).collect();
let spacer_len = if params.is_empty() {
0
Expand Down Expand Up @@ -453,6 +453,19 @@ impl DocFunction {
format!("def {}{}{}:\n{} pass", name, params, ret, docstring)
}

pub(crate) fn find_param_with_name(&self, param_name: &str) -> Option<&DocParam> {
self.params.iter().find(|p| match p {
DocParam::Arg { name, .. }
| DocParam::Args { name, .. }
| DocParam::Kwargs { name, .. }
if name == param_name =>
{
true
}
_ => false,
})
}

/// Parses function documentation out of a docstring
///
/// # Arguments
Expand Down Expand Up @@ -673,7 +686,7 @@ pub struct DocProperty {
}

impl DocProperty {
fn render_as_code(&self, name: &str) -> String {
pub(crate) fn render_as_code(&self, name: &str) -> String {
match (
&self.typ,
self.docs.as_ref().map(DocString::render_as_quoted_code),
Expand Down Expand Up @@ -734,7 +747,7 @@ pub struct DocObject {
}

impl DocObject {
fn render_as_code(&self, name: &str) -> String {
pub(crate) fn render_as_code(&self, name: &str) -> String {
let summary = self
.docs
.as_ref()
Expand Down Expand Up @@ -783,6 +796,55 @@ pub enum DocItem {
Property(DocProperty),
}

impl DocItem {
/// Get the underlying [`DocString`] for this item, if it exists.
pub fn get_doc_string(&self) -> Option<&DocString> {
match self {
DocItem::Module(m) => m.docs.as_ref(),
DocItem::Object(o) => o.docs.as_ref(),
DocItem::Function(f) => f.docs.as_ref(),
DocItem::Property(p) => p.docs.as_ref(),
}
}

/// Get the summary of the underlying [`DocString`] for this item, if it exists.
pub fn get_doc_summary(&self) -> Option<&str> {
self.get_doc_string().map(|ds| ds.summary.as_str())
}
}

impl DocMember {
/// Get the underlying [`DocString`] for this item, if it exists.
pub fn get_doc_string(&self) -> Option<&DocString> {
match self {
DocMember::Function(f) => f.docs.as_ref(),
DocMember::Property(p) => p.docs.as_ref(),
}
}

/// Get the summary of the underlying [`DocString`] for this item, if it exists.
pub fn get_doc_summary(&self) -> Option<&str> {
self.get_doc_string().map(|ds| ds.summary.as_str())
}
}

impl DocParam {
/// Get the underlying [`DocString`] for this item, if it exists.
pub fn get_doc_string(&self) -> Option<&DocString> {
match self {
DocParam::Arg { docs, .. }
| DocParam::Args { docs, .. }
| DocParam::Kwargs { docs, .. } => docs.as_ref(),
_ => None,
}
}

/// Get the summary of the underlying [`DocString`] for this item, if it exists.
pub fn get_doc_summary(&self) -> Option<&str> {
self.get_doc_string().map(|ds| ds.summary.as_str())
}
}

/// The main structure that represents the documentation for a given symbol / module.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Doc {
Expand Down
Loading

0 comments on commit 7c7d170

Please sign in to comment.