Skip to content

Commit

Permalink
fix: compile main path
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarrus1 committed Sep 3, 2023
1 parent 8b3ec88 commit 8f460dc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
16 changes: 15 additions & 1 deletion crates/sourcepawn_lsp/src/lsp_ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lsp_types::{notification::Notification, request::Request, TextDocumentIdentifier};
use lsp_types::{notification::Notification, request::Request, TextDocumentIdentifier, Url};
use serde::{Deserialize, Serialize};

pub enum PreprocessedDocument {}
Expand All @@ -15,6 +15,20 @@ pub struct PreprocessedDocumentParams {
pub text_document: Option<TextDocumentIdentifier>,
}

pub enum ProjectMainPath {}

impl Request for ProjectMainPath {
type Params = ProjectMainPathParams;
type Result = Url;
const METHOD: &'static str = "sourcepawn-lsp/projectMainPath";
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ProjectMainPathParams {
pub uri: Option<Url>,
}

pub enum ProjectsGraphviz {}

impl Request for ProjectsGraphviz {
Expand Down
2 changes: 2 additions & 0 deletions crates/sourcepawn_lsp/src/server/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod definition;
mod document_symbol;
mod hover;
mod preprocessed_document;
mod project_main_path;
mod projects_graphviz;
mod reference;
mod rename;
Expand Down Expand Up @@ -43,6 +44,7 @@ impl Server {
self.preprocessed_document(id, params)
})?
.on::<lsp_ext::ProjectsGraphviz, _>(|id, params| self.projects_graphviz(id, params))?
.on::<lsp_ext::ProjectMainPath, _>(|id, params| self.project_main_path(id, params))?
.default()
{
self.connection.sender.send(response.into())?;
Expand Down
33 changes: 33 additions & 0 deletions crates/sourcepawn_lsp/src/server/requests/project_main_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use anyhow::bail;
use lsp_server::RequestId;
use store::normalize_uri;

use crate::{lsp_ext::ProjectMainPathParams, Server};

impl Server {
pub(super) fn project_main_path(
&mut self,
id: RequestId,
params: ProjectMainPathParams,
) -> anyhow::Result<()> {
let Some(mut uri) = params.uri else {
bail!("No uri passed to command");
};
normalize_uri(&mut uri);
let Some(file_id) = self.store.read().path_interner.get(&uri) else {
bail!("No file ID found for URI {:?}", uri);
};
let Some(root_node) = self.store.read().projects.find_root_from_id(file_id) else {
bail!("No project root found for file ID {:?}", file_id);
};
let main_uri = self
.store
.read()
.path_interner
.lookup(root_node.file_id)
.clone();
self.run_query(id, move |_store| main_uri);

Ok(())
}
}
13 changes: 9 additions & 4 deletions editors/code/src/Commands/compileSM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { existsSync, mkdirSync } from "fs";
import { execFile } from "child_process";

import { run as uploadToServerCommand } from "./uploadToServer";
import { findMainPath } from "../spUtils";
import { run as refreshPluginsCommand } from "./refreshPlugins";
import { ctx } from "../spIndex";
import { ProjectMainPathParams, projectMainPath } from "../lsp_ext";

// Create an OutputChannel variable here but do not initialize yet.
let output: OutputChannel;
Expand All @@ -26,16 +26,21 @@ export async function run(args: URI): Promise<void> {
const uri = args === undefined ? window.activeTextEditor.document.uri : args;
const workspaceFolder = Workspace.getWorkspaceFolder(uri);

const mainPath = findMainPath(uri);
const alwaysCompileMainPath: boolean = Workspace.getConfiguration(
"sourcepawn",
workspaceFolder
).get<boolean>("MainPathCompilation");

// Decide which file to compile here.
let fileToCompilePath: string;
if (alwaysCompileMainPath && mainPath !== undefined && mainPath !== "") {
fileToCompilePath = mainPath;
if (alwaysCompileMainPath) {
const params: ProjectMainPathParams = { uri: uri.toString() };
const mainUri = await ctx?.client.sendRequest(projectMainPath, params);
if (mainUri === undefined) {
fileToCompilePath = uri.fsPath;
} else {
fileToCompilePath = URI.parse(mainUri).fsPath;
}
} else {
fileToCompilePath = uri.fsPath;
}
Expand Down
10 changes: 10 additions & 0 deletions editors/code/src/lsp_ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ export type PreprocessedDocumentParams = {
textDocument?: lc.TextDocumentIdentifier;
};

export const projectMainPath = new lc.RequestType<
ProjectMainPathParams,
lc.URI,
void
>("sourcepawn-lsp/projectMainPath");

export type ProjectMainPathParams = {
uri?: lc.URI;
};

export const projectsGraphviz = new lc.RequestType<
ProjectsGraphvizParams,
string,
Expand Down

0 comments on commit 8f460dc

Please sign in to comment.