-
Notifications
You must be signed in to change notification settings - Fork 8
Add source code and test helper in GraphTest #789
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,26 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use super::normalize_indentation; | ||
| #[cfg(test)] | ||
| use crate::diagnostic::Rule; | ||
| use crate::indexing::{self, LanguageId}; | ||
| use crate::model::graph::{Graph, NameDependent}; | ||
| use crate::model::ids::{NameId, StringId}; | ||
| use crate::model::ids::{DefinitionId, NameId, StringId}; | ||
| use crate::resolution::Resolver; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct GraphTest { | ||
| graph: Graph, | ||
| sources: HashMap<String, String>, | ||
| } | ||
|
|
||
| impl GraphTest { | ||
| #[must_use] | ||
| pub fn new() -> Self { | ||
| Self { graph: Graph::new() } | ||
| Self { | ||
| graph: Graph::new(), | ||
| sources: HashMap::new(), | ||
| } | ||
| } | ||
|
|
||
| #[must_use] | ||
|
|
@@ -31,12 +37,37 @@ impl GraphTest { | |
| pub fn index_uri(&mut self, uri: &str, source: &str) { | ||
| let source = normalize_indentation(source); | ||
| indexing::index_source(&mut self.graph, uri, &source, &LanguageId::Ruby); | ||
| self.sources.insert(uri.to_string(), source); | ||
| } | ||
|
|
||
| /// Indexes an RBS source | ||
| pub fn index_rbs_uri(&mut self, uri: &str, source: &str) { | ||
| let source = normalize_indentation(source); | ||
| indexing::index_source(&mut self.graph, uri, &source, &LanguageId::Rbs); | ||
| self.sources.insert(uri.to_string(), source); | ||
| } | ||
|
|
||
| /// Returns the normalized source for the given URI. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if the URI has not been indexed. | ||
| #[must_use] | ||
| pub fn source(&self, uri: &str) -> &str { | ||
| self.sources.get(uri).expect("source not found for URI") | ||
| } | ||
|
|
||
| /// Returns the source text for a definition, sliced by its offset. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if the definition or its document does not exist. | ||
| #[must_use] | ||
| pub fn source_of(&self, definition_id: &DefinitionId) -> &str { | ||
| let def = self.graph.definitions().get(definition_id).unwrap(); | ||
| let uri = self.graph.documents().get(def.uri_id()).unwrap().uri(); | ||
| let source = self.source(uri); | ||
| &source[def.offset().start() as usize..def.offset().end() as usize] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| pub fn delete_uri(&mut self, uri: &str) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this method work with definitions coming from
built_in.rs? They have uri but don't have document AFAIK. So it'll probably panic on this line?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. But I think we can easily avoid testing with builtins...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we'll intentionally test against the built in definitions. What could happen is that we build other helpers on top of this method, which takes a declaration and get all the sources of it. In that case it'd be reasonable to pass
Objectetc to it and then it'd explode.We can likely prevent it by checking if the definition is a built in before invoking this.