Skip to content
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

WIP: textDocument/documentSymbol (Outline) #75

Closed
wants to merge 14 commits into from
23 changes: 22 additions & 1 deletion vhdl_lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2018, Olof Kraigher [email protected]
// Copyright (c) 2020, Olof Kraigher [email protected]

// Allowing this, since box_patterns are feature gated: https://github.com/rust-lang/rfcs/pull/469
// Track here: https://github.com/rust-lang/rust/issues/29641
Expand Down Expand Up @@ -997,6 +997,9 @@ pub enum ContextItem {
pub struct ContextDeclaration {
pub ident: Ident,
pub items: ContextClause,

// Non-LRM fields
pub source_range: SrcPos,
}

/// LRM 4.9 Package instatiation declaration
Expand All @@ -1006,6 +1009,9 @@ pub struct PackageInstantiation {
pub ident: Ident,
pub package_name: WithPos<SelectedName>,
pub generic_map: Option<Vec<AssociationElement>>,

// Non-LRM fields
pub source_range: SrcPos,
}

/// LRM 7.3 Configuration specification
Expand Down Expand Up @@ -1093,6 +1099,9 @@ pub struct ConfigurationDeclaration {
pub decl: Vec<ConfigurationDeclarativeItem>,
pub vunit_bind_inds: Vec<VUnitBindingIndication>,
pub block_config: BlockConfiguration,

// Non-LRM fields
pub source_range: SrcPos,
}

/// LRM 3.2 Entity declarations
Expand All @@ -1104,6 +1113,9 @@ pub struct EntityDeclaration {
pub port_clause: Option<Vec<InterfaceDeclaration>>,
pub decl: Vec<Declaration>,
pub statements: Vec<LabeledConcurrentStatement>,

// Non-LRM fields
pub source_range: SrcPos,
}
/// LRM 3.3 Architecture bodies
#[derive(PartialEq, Debug, Clone)]
Expand All @@ -1113,6 +1125,9 @@ pub struct ArchitectureBody {
pub entity_name: WithRef<Ident>,
pub decl: Vec<Declaration>,
pub statements: Vec<LabeledConcurrentStatement>,

// Non-LRM fields
pub source_range: SrcPos,
}

/// LRM 4.7 Package declarations
Expand All @@ -1122,6 +1137,9 @@ pub struct PackageDeclaration {
pub ident: Ident,
pub generic_clause: Option<Vec<InterfaceDeclaration>>,
pub decl: Vec<Declaration>,

// Non-LRM fields
pub source_range: SrcPos,
}

/// LRM 4.8 Package bodies
Expand All @@ -1130,6 +1148,9 @@ pub struct PackageBody {
pub context_clause: ContextClause,
pub ident: WithRef<Ident>,
pub decl: Vec<Declaration>,

// Non-LRM fields
pub source_range: SrcPos,
}

/// LRM 13.1 Design units
Expand Down
9 changes: 3 additions & 6 deletions vhdl_lang/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2018, Olof Kraigher [email protected]
// Copyright (c) 2020, Olof Kraigher [email protected]

//! Configuration of the design hierarchy and other settings

use toml;

use self::fnv::FnvHashMap;
use self::toml::Value;
use crate::data::*;
use fnv;
use fnv::FnvHashMap;
use std::env;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;
use toml::Value;

#[derive(Clone, PartialEq, Default, Debug)]
pub struct Config {
Expand Down
5 changes: 2 additions & 3 deletions vhdl_lang/src/data/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2018, Olof Kraigher [email protected]
// Copyright (c) 2020, Olof Kraigher [email protected]

use super::contents::Contents;
use super::diagnostic::{Diagnostic, DiagnosticResult};
use pad;
use parking_lot::{RwLock, RwLockReadGuard};
use std::cmp::{max, min};
use std::collections::hash_map::DefaultHasher;
Expand Down Expand Up @@ -399,7 +398,7 @@ impl SrcPos {
context_lines: u32,
) -> (usize, String) {
let lines = self.get_line_context(context_lines, contents);
use self::pad::{Alignment, PadStr};
use pad::{Alignment, PadStr};
// +1 since lines are shown with 1-index
let lineno_len = (self.range.start.line + context_lines + 1)
.to_string()
Expand Down
5 changes: 2 additions & 3 deletions vhdl_lang/src/data/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2018, Olof Kraigher [email protected]
// Copyright (c) 2020, Olof Kraigher [email protected]

use super::latin_1::Latin1String;
use parking_lot::RwLock;
use std::sync::Arc;

use self::fnv::FnvHashMap;
use fnv;
use fnv::FnvHashMap;

/// Represents a unique string symbol.
///
Expand Down
40 changes: 26 additions & 14 deletions vhdl_lang/src/syntax/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2018, Olof Kraigher [email protected]
// Copyright (c) 2020, Olof Kraigher [email protected]

use super::common::error_on_end_identifier_mismatch;
use super::common::ParseResult;
Expand Down Expand Up @@ -268,7 +268,7 @@ pub fn parse_configuration_declaration(
stream: &mut TokenStream,
diagnostics: &mut dyn DiagnosticHandler,
) -> ParseResult<ConfigurationDeclaration> {
stream.expect_kind(Configuration)?;
let configuration_token = stream.expect_kind(Configuration)?;
let ident = stream.expect_ident()?;
stream.expect_kind(Of)?;
let entity_name = parse_selected_name(stream)?;
Expand Down Expand Up @@ -301,14 +301,15 @@ pub fn parse_configuration_declaration(
if let Some(diagnostic) = error_on_end_identifier_mismatch(&ident, &end_ident) {
diagnostics.push(diagnostic)
}
stream.expect_kind(SemiColon)?;
let semi_token = stream.expect_kind(SemiColon)?;
Ok(ConfigurationDeclaration {
context_clause: ContextClause::default(),
ident,
entity_name,
decl,
vunit_bind_inds,
block_config,
source_range: configuration_token.pos.combine_into(&semi_token),
})
}

Expand Down Expand Up @@ -351,7 +352,8 @@ pub fn parse_configuration_specification(
#[cfg(test)]
mod tests {
use super::*;
use crate::syntax::test::Code;
use crate::syntax::test::{source_range, Code};
use pretty_assertions::assert_eq;

#[test]
fn empty_configuration() {
Expand All @@ -375,7 +377,8 @@ end;
block_spec: code.s1("rtl(0)").name(),
use_clauses: vec![],
items: vec![],
}
},
source_range: source_range(&code, (0, 0), (3, 4))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using hard coded line numbers and colums makes the tests fragile and harder to maintain. I have avoided it so far by using test helper to compute source ranges from textual searches such as finding the nth substring. In this case it would be nice to create a test helper that finds a source range between two substrings instead of hardcoding it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

}
);
}
Expand All @@ -402,7 +405,8 @@ end configuration cfg;
block_spec: code.s1("rtl(0)").name(),
use_clauses: vec![],
items: vec![],
}
},
source_range: source_range(&code, (0, 0), (3, 22)),
}
);
}
Expand Down Expand Up @@ -433,7 +437,8 @@ end configuration cfg;
block_spec: code.s1("rtl(0)").name(),
use_clauses: vec![],
items: vec![],
}
},
source_range: source_range(&code, (0, 0), (5, 22)),
}
);
}
Expand Down Expand Up @@ -466,7 +471,8 @@ end configuration cfg;
block_spec: code.s1("rtl(0)").name(),
use_clauses: vec![],
items: vec![],
}
},
source_range: source_range(&code, (0, 0), (5, 22)),
}
);
}
Expand All @@ -493,7 +499,8 @@ end configuration cfg;
block_spec: code.s1("rtl(0)").name(),
use_clauses: vec![],
items: vec![],
}
},
source_range: source_range(&code, (0, 0), (3, 22)),
}
);
}
Expand Down Expand Up @@ -535,7 +542,8 @@ end configuration cfg;
items: vec![],
})
],
}
},
source_range: source_range(&code, (0, 0), (7, 22)),
}
);
}
Expand Down Expand Up @@ -580,7 +588,8 @@ end configuration cfg;
items: vec![],
}),
}),],
}
},
source_range: source_range(&code, (0, 0), (7, 22)),
}
);
}
Expand Down Expand Up @@ -636,7 +645,8 @@ end configuration cfg;
items: vec![],
}),
}),],
}
},
source_range: source_range(&code, (0, 0), (9, 22)),
}
);
}
Expand Down Expand Up @@ -683,7 +693,8 @@ end configuration cfg;
vunit_bind_inds: Vec::new(),
block_config: None,
}),],
}
},
source_range: source_range(&code, (0, 0), (6, 22)),
}
);
}
Expand Down Expand Up @@ -761,7 +772,8 @@ end configuration cfg;
block_config: None,
})
],
}
},
source_range: source_range(&code, (0, 0), (11, 22)),
}
);
}
Expand Down
27 changes: 19 additions & 8 deletions vhdl_lang/src/syntax/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2018, Olof Kraigher [email protected]
// Copyright (c) 2020, Olof Kraigher [email protected]

use super::common::error_on_end_identifier_mismatch;
use super::common::ParseResult;
Expand Down Expand Up @@ -104,19 +104,19 @@ pub fn parse_context(
End => {
stream.pop_if_kind(Context)?;
end_ident = stream.pop_optional_ident()?;
stream.expect_kind(SemiColon)?;
break;
}
)
}

let semi_token = stream.expect_kind(SemiColon)?;
let ident = to_simple_name(name)?;

diagnostics.push_some(error_on_end_identifier_mismatch(&ident, &end_ident));

Ok(DeclarationOrReference::Declaration(ContextDeclaration {
ident,
items,
source_range: context_token.pos.combine_into(&semi_token),
}))
} else {
// Context reference
Expand All @@ -140,7 +140,8 @@ mod tests {
use super::*;

use crate::data::Diagnostic;
use crate::syntax::test::Code;
use crate::syntax::test::{source_range, Code};
use pretty_assertions::assert_eq;

#[test]
fn test_library_clause_single_name() {
Expand Down Expand Up @@ -246,13 +247,21 @@ context ident is
end context ident;
",
];
for variant in variants {
let source_ranges = vec![
((0, 0), (1, 4)),
((0, 0), (1, 12)),
((0, 0), (1, 10)),
((0, 0), (1, 18)),
];
for variant in variants.iter().zip(source_ranges.iter()) {
let (variant, (start, end)) = variant;
let code = Code::new(variant);
assert_eq!(
code.with_stream_no_diagnostics(parse_context),
DeclarationOrReference::Declaration(ContextDeclaration {
ident: code.s1("ident").ident(),
items: vec![]
items: vec![],
source_range: source_range(&code, *start, *end),
})
);
}
Expand All @@ -278,7 +287,8 @@ end context ident2;
context,
DeclarationOrReference::Declaration(ContextDeclaration {
ident: code.s1("ident").ident(),
items: vec![]
items: vec![],
source_range: source_range(&code, (0, 0), (1, 19)),
})
);
}
Expand Down Expand Up @@ -317,7 +327,8 @@ end context;
}),
code.s1("context foo.ctx;")
),
]
],
source_range: source_range(&code, (0, 0), (4, 12)),
})
)
}
Expand Down
Loading