Skip to content

rename to cvldoc, add raw field #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 41 additions & 28 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ jobs:
pip3 install maturin
cd src/python_wrapper
rustup target install x86_64-apple-darwin aarch64-apple-darwin
maturin build --target=aarch64-apple-darwin --universal2 --bindings=pyo3
maturin build --target=aarch64-apple-darwin --universal2
- store_artifacts:
path: /Users/distiller/project/src/python_wrapper/target/wheels
- run:
name: "Upload to Test PyPI"
command: |
pip3 install --user twine
export PATH=/Users/distiller/Library/Python/3.9/bin:${PATH}
python3 -m twine upload --skip-existing --verbose --repository testpypi -u ${TESTPYPI_USER} -p ${TESTPYPI_PASSWORD} /Users/distiller/project/src/python_wrapper/target/wheels/*
path: ./src/python_wrapper/target/wheels
- persist_to_workspace:
root: ./src/python_wrapper/target
paths:
- wheels/*

build-linux-wheels:
docker:
Expand All @@ -37,18 +35,16 @@ jobs:
- run:
name: "Install Maturin and Build Wheels"
command: |
for PYBIN in /opt/python/cp3{7,8,9,10,11}*/bin; do
"${PYBIN}/pip" install maturin
"${PYBIN}/maturin" build -m src/python_wrapper/Cargo.toml -i "${PYBIN}/python"
# "${PYBIN}/maturin" build -m src/python_wrapper/Cargo.toml -i "${PYBIN}/python --target x86_64-pc-windows-gnu"
done
cd src/python_wrapper
PYBIN_DIR="/opt/python/cp37-cp37m/bin"
"${PYBIN_DIR}/pip" install maturin
"${PYBIN_DIR}/maturin" build --bindings=pyo3
- store_artifacts:
path: /root/project/src/python_wrapper/target/wheels/
- run:
name: "Upload to Test PyPI"
command: |
/opt/python/cp310-cp310/bin/pip install --user twine
/opt/python/cp310-cp310/bin/python -m twine upload --skip-existing --verbose --repository testpypi -u ${TESTPYPI_USER} -p ${TESTPYPI_PASSWORD} /root/project/src/python_wrapper/target/wheels/*
path: ./src/python_wrapper/target/wheels/
- persist_to_workspace:
root: ./src/python_wrapper/target
paths:
- wheels/*

build-windows-wheels:
executor: win/default
Expand All @@ -68,8 +64,8 @@ jobs:
- run:
name: Fix Cargo Config
command: |
echo "[net]" >> ~/.cargo/config
echo "git-fetch-with-cli = true" >> ~/.cargo/config
echo "[net]" >> ~/.cargo/config
echo "git-fetch-with-cli = true" >> ~/.cargo/config
shell: bash.exe
- run:
name: Build Wheels
Expand All @@ -80,18 +76,35 @@ jobs:
maturin build --target=x86_64-pc-windows-msvc --bindings=pyo3
shell: bash.exe
- store_artifacts:
path: .\src\python_wrapper\target\wheels
path: ./src/python_wrapper/target/wheels
- persist_to_workspace:
root: ./src/python_wrapper/target
paths:
- wheels/*

publish-to-testpypi:
docker:
- image: cimg/python:3.10.7
steps:
- attach_workspace:
at: .
- run:
name: "Upload to Test PyPI"
name: "Upload wheels to Test PyPI"
command: |
pip install --user twine
# export PATH=${HOME}/AppData/Roaming/Python/Python37/Scripts:${PATH}
python -m twine upload --skip-existing --verbose --repository testpypi -u ${TESTPYPI_USER} -p ${TESTPYPI_PASSWORD} src/python_wrapper/target/wheels/*
shell: bash.exe
for wheel in wheels/*.whl; do
echo "Uploading $wheel...";
python -m twine upload --skip-existing --verbose --repository testpypi -u ${TESTPYPI_USER} -p ${TESTPYPI_PASSWORD} "${wheel}"
done

workflows:
build-and-publish:
build-and-integration-test:
jobs:
- build-linux-wheels
- build-macos-wheels
- build-windows-wheels
- build-windows-wheels
- publish-to-testpypi:
requires:
- build-linux-wheels
- build-macos-wheels
- build-windows-wheels
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Changelog
## [0.4.0] - 2022-09-21
### Changed
- Changed all references from "NatSpec" to the new name, "CVLDoc"
- Internal restructure of documentation struct to refacator redundant fields
### Added
- `raw` field is now captured, containing the entire text from beginning of capture

## [0.3.2] - 2022-09-15
### Changed
- Updated dependencies
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "natspec_parser"
version = "0.3.2"
name = "cvldoc_parser"
version = "0.4.0"
edition = "2021"

[dependencies]
Expand Down
15 changes: 5 additions & 10 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{AssociatedElement, NatSpec, Tag};
use crate::{AssociatedElement, CvlDoc, Tag, DocData};
use lsp_types::{Diagnostic, DiagnosticSeverity, Range};

impl AssociatedElement {
Expand Down Expand Up @@ -34,22 +34,17 @@ impl AssociatedElement {
}
}

impl NatSpec {
impl CvlDoc {
pub fn enumerate_diagnostics(&self) -> Vec<Diagnostic> {
let mut warnings = Vec::new();

if let NatSpec::Documentation {
tags,
associated,
range: natspec_range,
} = self
{
if let DocData::Documentation { tags, associated } = &self.data {
const WARNING: DiagnosticSeverity = DiagnosticSeverity::WARNING;
const ERROR: DiagnosticSeverity = DiagnosticSeverity::ERROR;

let mut add = |message: String, range: Option<Range>, severity: DiagnosticSeverity| {
let mut add = |message: String, diag_range: Option<Range>, severity: DiagnosticSeverity| {
let diag = Diagnostic {
range: range.unwrap_or(*natspec_range),
range: diag_range.unwrap_or(self.range),
severity: Some(severity),
message,
..Default::default()
Expand Down
54 changes: 30 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NatSpec {
FreeForm {
text: String,
range: Range,
},

pub enum DocData {
FreeForm(String),
Documentation {
tags: Vec<DocumentationTag>,
associated: Option<AssociatedElement>,
range: Range,
},
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CvlDoc {
pub raw: String,
pub range: Range,
pub data: DocData,
}

pub type Ty = String;
pub type Param = (Ty, Option<String>);

Expand Down Expand Up @@ -71,36 +73,40 @@ pub enum AssociatedElement {
},
}

impl NatSpec {
impl CvlDoc {
pub fn from_rope(rope: Rope) -> Vec<CvlDoc> {
let converter = RangeConverter::new(rope.clone());
let builders = {
let src = rope.to_string();
let (parse, _) = parser().parse_recovery(src.as_str());
parse.unwrap_or_default()
};

builders
.into_iter()
.filter_map(|builder| builder.build(converter.clone(), rope.clone()).ok())
.collect()
}
}

impl DocData {
pub fn tags(&self) -> Option<&[DocumentationTag]> {
match self {
NatSpec::Documentation { tags, .. } => Some(tags),
DocData::Documentation { tags, .. } => Some(tags),
_ => None,
}
}

pub fn associated_element(&self) -> Option<&AssociatedElement> {
match self {
NatSpec::Documentation { associated, .. } => associated.as_ref(),
DocData::Documentation { associated, .. } => associated.as_ref(),
_ => None,
}
}

pub fn from_rope(rope: Rope) -> Vec<NatSpec> {
let src = rope.to_string();
let converter = RangeConverter::new(rope);
let (builders, _) = parser().parse_recovery(src.as_str());

builders
.unwrap_or_default()
.into_iter()
.filter_map(|(builder, _)| builder.build_with_converter(converter.clone()).ok())
.collect()
}

pub fn auto_generated_title(&self) -> Result<String, Report> {
match self {
NatSpec::Documentation { associated, .. } => {
DocData::Documentation { associated, .. } => {
let associated = associated
.as_ref()
.ok_or_else(|| eyre!("documentation has no associated syntactic element"))?;
Expand Down Expand Up @@ -128,7 +134,7 @@ impl NatSpec {
}

pub fn is_documentation(&self) -> bool {
matches!(self, NatSpec::Documentation { .. })
matches!(self, DocData::Documentation { .. })
}
}

Expand Down
57 changes: 30 additions & 27 deletions src/parse/builder.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
use crate::util::span_to_range::{RangeConverter, Span, Spanned};
use crate::{AssociatedElement, DocumentationTag, NatSpec, Tag};
use color_eyre::eyre::bail;
use crate::{AssociatedElement, CvlDoc, DocData, DocumentationTag, Tag};
use color_eyre::eyre::{bail, ensure, eyre};
use color_eyre::Report;
use ropey::Rope;

#[derive(Debug, Clone)]
pub enum NatSpecBuilder {
pub enum CvlDocBuilder {
FreeFormComment {
text: String,
span: Span,
},
Documentation {
spanned_body: Vec<Spanned<String>>,
element_under_doc: Option<AssociatedElement>,
associated: Option<AssociatedElement>,
span: Span,
},
CommentedOutBlock,
CommentedOutLine,
ParseError,
}

impl NatSpecBuilder {
pub fn build_with_converter(self, converter: RangeConverter) -> Result<NatSpec, Report> {
impl CvlDocBuilder {
fn raw_data(src: Rope, span: Span) -> Result<String, Report> {
src.get_slice(span)
.map(String::from)
.ok_or_else(|| eyre!("span is outside of file bounds"))
}

pub fn build(self, converter: RangeConverter, src: Rope) -> Result<CvlDoc, Report> {
match self {
NatSpecBuilder::FreeFormComment { text, span } => {
let free_form = NatSpec::FreeForm {
text,
CvlDocBuilder::FreeFormComment { text, span } => {
let cvl_doc = CvlDoc {
raw: CvlDocBuilder::raw_data(src, span.clone())?,
range: converter.to_range(span),
data: DocData::FreeForm(text),
};
Ok(free_form)
Ok(cvl_doc)
}
NatSpecBuilder::Documentation {
CvlDocBuilder::Documentation {
spanned_body,
element_under_doc,
associated,
span,
} => {
if spanned_body.is_empty() {
bail!("documentation has no body");
}
let tags = NatSpecBuilder::process_doc_body(&spanned_body, converter.clone());

let associated = element_under_doc.map(AssociatedElement::from);
ensure!(!spanned_body.is_empty(), "documentation has no body");
let tags = CvlDocBuilder::process_doc_body(&spanned_body, converter.clone());

let range = converter.to_range(span);

Ok(NatSpec::Documentation {
tags,
associated,
range,
})
let cvl_doc = CvlDoc {
raw: CvlDocBuilder::raw_data(src, span.clone())?,
range: converter.to_range(span),
data: DocData::Documentation { tags, associated },
};
Ok(cvl_doc)
}
NatSpecBuilder::CommentedOutBlock | NatSpecBuilder::CommentedOutLine => {
CvlDocBuilder::CommentedOutBlock | CvlDocBuilder::CommentedOutLine => {
bail!("currently commented out code is not parsed")
}
NatSpecBuilder::ParseError => bail!("parse errors can not be converted"),
CvlDocBuilder::ParseError => bail!("parse errors can not be converted"),
}
}

Expand Down
Loading