Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from Sarrus1/feature/reference-provider
Browse files Browse the repository at this point in the history
Feature/reference provider
  • Loading branch information
Sarrus1 authored Jan 31, 2023
2 parents 7fa32e7 + ee5d424 commit 9129166
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 21 deletions.
45 changes: 42 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,51 @@ jobs:
fail-fast: false
matrix:
include:
- target: x86_64-pc-windows-gnu
- os: windows-latest
target: x86_64-pc-windows-msvc
program: cargo
archive: zip
- target: x86_64-unknown-linux-musl

- os: windows-latest
target: i686-pc-windows-msvc
program: cargo
archive: zip

- os: windows-latest
target: aarch64-pc-windows-msvc
program: cargo
archive: zip

- os: macos-latest
target: x86_64-apple-darwin
program: cargo
archive: zip

- os: macos-latest
target: aarch64-apple-darwin
program: cargo
archive: zip
- target: x86_64-apple-darwin

- os: ubuntu-20.04
target: x86_64-unknown-linux-gnu
program: cargo
archive: zip

- os: ubuntu-20.04
target: aarch64-unknown-linux-gnu
program: cross
archive: zip

- os: ubuntu-20.04
target: armv7-unknown-linux-gnueabihf
program: cross
archive: zip

- os: ubuntu-20.04
target: x86_64-unknown-linux-musl
program: cargo
archive: zip

steps:
- uses: actions/checkout@master
- name: Compile and release
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Release Notes

## [0.3.2]
## [0.4.0]

### Added

- Add Reference provider.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sourcepawn_lsp"
version = "0.3.1"
version = "0.4.0"
edition = "2021"
description = "Language Server implention for the SourcePawn programming language."
license = "GPL-3.0"
Expand Down
18 changes: 6 additions & 12 deletions src/parser/function_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,16 @@ fn build_detail(
visibility_node: Option<Node>,
definition_type_node: Option<Node>,
) -> Result<String, Utf8Error> {
let mut detail = format!("{} {}", type_?.to_string(), name?.to_string());
if params_node.is_some() {
detail.push_str(
params_node
.unwrap()
.utf8_text(document.text.as_bytes())
.unwrap(),
);
let mut detail = format!("{} {}", type_?, name?);
if let Some(params_node) = params_node {
detail.push_str(params_node.utf8_text(document.text.as_bytes()).unwrap());
}
if visibility_node.is_some() {
detail = format!(
"{} {}",
visibility_node
.unwrap()
.utf8_text(document.text.as_bytes())?
.to_string(),
.utf8_text(document.text.as_bytes())?,
detail
)
}
Expand Down Expand Up @@ -271,10 +265,10 @@ fn read_function_parameters(
}

fn extract_param_doc(name: &str, documentation: &Description) -> Option<String> {
let re = Regex::new(&format!("@param\\s+(?:\\b{}\\b)([^@]+)", name.to_string())).unwrap();
let re = Regex::new(&format!("@param\\s+(?:\\b{}\\b)([^@]+)", name)).unwrap();
if let Some(caps) = re.captures(&documentation.text) {
if let Some(text) = caps.get(1) {
return Some(text.as_str().replace("*", "").trim().to_string());
return Some(text.as_str().replace('*', "").trim().to_string());
}
};

Expand Down
1 change: 1 addition & 0 deletions src/providers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod completion;
pub mod definition;
pub mod hover;
pub mod reference;
pub mod semantic_tokens;
pub mod signature_help;

Expand Down
33 changes: 33 additions & 0 deletions src/providers/reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use lsp_types::{Location, ReferenceParams};

use crate::spitem::get_items_from_position;

use super::FeatureRequest;

/// Build a vector of [Locations](lsp_types::Location) from a [ReferenceParams](lsp_types::ReferenceParams).
///
/// # Arguments
///
/// * `request` - Reference request object [FeatureRequest<ReferenceParams>].
pub fn provide_reference(request: FeatureRequest<ReferenceParams>) -> Option<Vec<Location>> {
let items = get_items_from_position(
&request.store,
request.params.text_document_position.position,
request
.params
.text_document_position
.text_document
.uri
.clone(),
);
let mut locations = vec![];
for item in items {
let item = item.lock().unwrap();
let references = item.references();
if let Some(references) = references {
locations.extend(references.clone());
}
}

Some(locations.iter().map(|loc| loc.to_lsp_location()).collect())
}
17 changes: 14 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use lsp_types::{
DidChangeConfiguration, DidChangeTextDocument, DidOpenTextDocument, ShowMessage,
},
request::{
Completion, GotoDefinition, HoverRequest, SemanticTokensFullRequest, SignatureHelpRequest,
WorkspaceConfiguration,
Completion, GotoDefinition, HoverRequest, References, SemanticTokensFullRequest,
SignatureHelpRequest, WorkspaceConfiguration,
},
CompletionOptions, CompletionParams, ConfigurationItem, ConfigurationParams,
DidChangeConfigurationParams, DidChangeTextDocumentParams, DidOpenTextDocumentParams,
GotoDefinitionParams, HoverParams, HoverProviderCapability, InitializeParams, MessageType,
OneOf, SemanticTokenModifier, SemanticTokenType, SemanticTokensFullOptions,
OneOf, ReferenceParams, SemanticTokenModifier, SemanticTokenType, SemanticTokensFullOptions,
SemanticTokensLegend, SemanticTokensOptions, SemanticTokensParams,
SemanticTokensServerCapabilities, ServerCapabilities, ShowMessageParams, SignatureHelpOptions,
SignatureHelpParams, TextDocumentSyncCapability, TextDocumentSyncKind, Url,
Expand Down Expand Up @@ -163,6 +163,7 @@ impl Server {
retrigger_characters: Some(vec![",".to_string(), "(".to_string()]),
..Default::default()
}),
references_provider: Some(OneOf::Left(true)),
semantic_tokens_provider: Some(
SemanticTokensServerCapabilities::SemanticTokensOptions(SemanticTokensOptions {
work_done_progress_options: WorkDoneProgressOptions {
Expand Down Expand Up @@ -443,6 +444,15 @@ impl Server {
Ok(())
}

fn reference(&mut self, id: RequestId, mut params: ReferenceParams) -> anyhow::Result<()> {
utils::normalize_uri(&mut params.text_document_position.text_document.uri);
let uri = Arc::new(params.text_document_position.text_document.uri.clone());
self.read_unscanned_document(uri.clone());

self.handle_feature_request(id, params, uri, providers::reference::provide_reference)?;
Ok(())
}

fn handle_feature_request<P, R, H>(
&self,
id: RequestId,
Expand Down Expand Up @@ -491,6 +501,7 @@ impl Server {
.on::<GotoDefinition, _>(|id, params| self.definition(id, params))?
.on::<SemanticTokensFullRequest, _>(|id, params| self.semantic_tokens(id, params))?
.on::<SignatureHelpRequest, _>(|id, params| self.signature_help(id, params))?
.on::<References, _>(|id, params| self.reference(id, params))?

.default()
{
Expand Down
9 changes: 9 additions & 0 deletions src/spitem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ pub struct Location {
pub range: Range,
}

impl Location {
pub fn to_lsp_location(&self) -> lsp_types::Location {
lsp_types::Location {
uri: self.uri.as_ref().clone(),
range: self.range,
}
}
}

#[derive(Debug, Clone)]
/// Generic representation of an item, which can be converted to a
/// [CompletionItem](lsp_types::CompletionItem), [Location](lsp_types::Location), etc.
Expand Down

0 comments on commit 9129166

Please sign in to comment.