Skip to content

Commit a15f87e

Browse files
ndmitchellfacebook-github-bot
authored andcommitted
Add completions and hover
Summary: This is code from facebook#68, with cleanups. Differential Revision: D47208501 fbshipit-source-id: 4baf8714123b08de3e111c8002669ca4d673383a
1 parent d21c3ad commit a15f87e

File tree

11 files changed

+1801
-171
lines changed

11 files changed

+1801
-171
lines changed

starlark/src/docs/markdown.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
use std::slice;
19+
1820
use dupe::Dupe;
1921
use itertools::Itertools;
2022
use starlark_map::small_map::SmallMap;
@@ -238,15 +240,26 @@ fn render_object(name: &str, object: &DocObject) -> String {
238240
render_members(name, true, &object.docs, &object.members)
239241
}
240242

241-
fn render_doc_item(name: &str, item: &DocItem) -> String {
242-
match &item {
243+
pub(crate) fn render_doc_item(name: &str, item: &DocItem) -> String {
244+
match item {
243245
DocItem::Module(m) => render_module(name, m),
244246
DocItem::Object(o) => render_object(name, o),
245247
DocItem::Function(f) => render_function(name, f),
246248
DocItem::Property(p) => render_property(name, p),
247249
}
248250
}
249251

252+
pub(crate) fn render_doc_member(name: &str, item: &DocMember) -> String {
253+
match item {
254+
DocMember::Function(f) => render_function(name, f),
255+
DocMember::Property(p) => render_property(name, p),
256+
}
257+
}
258+
259+
pub(crate) fn render_doc_param(item: &DocParam) -> String {
260+
render_function_parameters(slice::from_ref(item)).unwrap_or_default()
261+
}
262+
250263
impl RenderMarkdown for Doc {
251264
fn render_markdown_opt(&self, flavor: MarkdownFlavor) -> Option<String> {
252265
match flavor {

starlark/src/docs/mod.rs

+67-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// TODO(nga): document it
2121
#![allow(missing_docs)]
2222

23-
mod markdown;
23+
pub(crate) mod markdown;
2424

2525
use std::collections::HashMap;
2626

@@ -349,7 +349,7 @@ pub struct DocModule {
349349
}
350350

351351
impl DocModule {
352-
fn render_as_code(&self) -> String {
352+
pub(crate) fn render_as_code(&self) -> String {
353353
let mut res = self
354354
.docs
355355
.as_ref()
@@ -425,7 +425,7 @@ impl DocFunction {
425425
}
426426
}
427427

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

456+
pub(crate) fn find_param_with_name(&self, param_name: &str) -> Option<&DocParam> {
457+
self.params.iter().find(|p| match p {
458+
DocParam::Arg { name, .. }
459+
| DocParam::Args { name, .. }
460+
| DocParam::Kwargs { name, .. }
461+
if name == param_name =>
462+
{
463+
true
464+
}
465+
_ => false,
466+
})
467+
}
468+
456469
/// Parses function documentation out of a docstring
457470
///
458471
/// # Arguments
@@ -673,7 +686,7 @@ pub struct DocProperty {
673686
}
674687

675688
impl DocProperty {
676-
fn render_as_code(&self, name: &str) -> String {
689+
pub(crate) fn render_as_code(&self, name: &str) -> String {
677690
match (
678691
&self.typ,
679692
self.docs.as_ref().map(DocString::render_as_quoted_code),
@@ -734,7 +747,7 @@ pub struct DocObject {
734747
}
735748

736749
impl DocObject {
737-
fn render_as_code(&self, name: &str) -> String {
750+
pub(crate) fn render_as_code(&self, name: &str) -> String {
738751
let summary = self
739752
.docs
740753
.as_ref()
@@ -783,6 +796,55 @@ pub enum DocItem {
783796
Property(DocProperty),
784797
}
785798

799+
impl DocItem {
800+
/// Get the underlying [`DocString`] for this item, if it exists.
801+
pub fn get_doc_string(&self) -> Option<&DocString> {
802+
match self {
803+
DocItem::Module(m) => m.docs.as_ref(),
804+
DocItem::Object(o) => o.docs.as_ref(),
805+
DocItem::Function(f) => f.docs.as_ref(),
806+
DocItem::Property(p) => p.docs.as_ref(),
807+
}
808+
}
809+
810+
/// Get the summary of the underlying [`DocString`] for this item, if it exists.
811+
pub fn get_doc_summary(&self) -> Option<&str> {
812+
self.get_doc_string().map(|ds| ds.summary.as_str())
813+
}
814+
}
815+
816+
impl DocMember {
817+
/// Get the underlying [`DocString`] for this item, if it exists.
818+
pub fn get_doc_string(&self) -> Option<&DocString> {
819+
match self {
820+
DocMember::Function(f) => f.docs.as_ref(),
821+
DocMember::Property(p) => p.docs.as_ref(),
822+
}
823+
}
824+
825+
/// Get the summary of the underlying [`DocString`] for this item, if it exists.
826+
pub fn get_doc_summary(&self) -> Option<&str> {
827+
self.get_doc_string().map(|ds| ds.summary.as_str())
828+
}
829+
}
830+
831+
impl DocParam {
832+
/// Get the underlying [`DocString`] for this item, if it exists.
833+
pub fn get_doc_string(&self) -> Option<&DocString> {
834+
match self {
835+
DocParam::Arg { docs, .. }
836+
| DocParam::Args { docs, .. }
837+
| DocParam::Kwargs { docs, .. } => docs.as_ref(),
838+
_ => None,
839+
}
840+
}
841+
842+
/// Get the summary of the underlying [`DocString`] for this item, if it exists.
843+
pub fn get_doc_summary(&self) -> Option<&str> {
844+
self.get_doc_string().map(|ds| ds.summary.as_str())
845+
}
846+
}
847+
786848
/// The main structure that represents the documentation for a given symbol / module.
787849
#[derive(Debug, Clone, PartialEq, Serialize)]
788850
pub struct Doc {

0 commit comments

Comments
 (0)