Skip to content

Commit

Permalink
Fix katas using package type setting (#517)
Browse files Browse the repository at this point in the history
This is a workaround to fix kata behavior in the playground, where they
were incorrectly treated as exe documents and failed compilation due to
missing entry point. The katas need to be treated as if they are lib
document. This change adds a new parameter to `updateDocument` on the
language server so that the caller can indicate what type the document
is.

Fixes #516

---------

Co-authored-by: Bill Ticehurst <[email protected]>
  • Loading branch information
swernli and billti authored Jul 31, 2023
1 parent a2a2f87 commit be9b870
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
11 changes: 9 additions & 2 deletions language_service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
completion::CompletionList, definition::Definition, hover::Hover, qsc_utils::compile_document,
};
use log::trace;
use qsc::PackageType;
use qsc_utils::Compilation;
use std::collections::HashMap;

Expand Down Expand Up @@ -50,9 +51,15 @@ impl<'a> LanguageService<'a> {
/// This should be called before any language service requests have been made
/// for the document, typically when the document is first opened in the editor.
/// It should also be called whenever the source code is updated.
pub fn update_document(&mut self, uri: &str, version: u32, text: &str) {
pub fn update_document(
&mut self,
uri: &str,
version: u32,
text: &str,
package_type: PackageType,
) {
trace!("update_document: {uri:?} {version:?}");
let compilation = compile_document(uri, text);
let compilation = compile_document(uri, text, package_type);
let errors = compilation.errors.clone();

// insert() will update the value if the key already exists
Expand Down
14 changes: 7 additions & 7 deletions language_service/src/qsc_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ pub(crate) struct Compilation {
pub errors: Vec<Error>,
}

pub(crate) fn compile_document(source_name: &str, source_contents: &str) -> Compilation {
pub(crate) fn compile_document(
source_name: &str,
source_contents: &str,
package_type: PackageType,
) -> Compilation {
let mut package_store = PackageStore::new(compile::core());
let std_package_id = package_store.insert(compile::std(&package_store));

// Source map only contains the current document.
let source_map = SourceMap::new([(source_name.into(), source_contents.into())], None);
let (unit, errors) = compile::compile(
&package_store,
&[std_package_id],
source_map,
PackageType::Exe,
);
let (unit, errors) =
compile::compile(&package_store, &[std_package_id], source_map, package_type);
Compilation {
package_store,
std_package_id,
Expand Down
2 changes: 1 addition & 1 deletion npm/src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Compiler implements ICompiler {
diags = errors;
}
);
languageService.update_document("code", 1, code);
languageService.update_document("code", 1, code, true /* exe */);
return mapDiagnostics(diags, code);
}

Expand Down
12 changes: 9 additions & 3 deletions npm/src/language-service/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export type LanguageServiceEvent = {
// These need to be async/promise results for when communicating across a WebWorker, however
// for running the compiler in the same thread the result will be synchronous (a resolved promise).
export interface ILanguageService {
updateDocument(uri: string, version: number, code: string): Promise<void>;
updateDocument(
uri: string,
version: number,
code: string,
isExe: boolean
): Promise<void>;
closeDocument(uri: string): Promise<void>;
getCompletions(documentUri: string, offset: number): Promise<ICompletionList>;
getHover(documentUri: string, offset: number): Promise<IHover | null>;
Expand Down Expand Up @@ -72,10 +77,11 @@ export class QSharpLanguageService implements ILanguageService {
async updateDocument(
documentUri: string,
version: number,
code: string
code: string,
isExe: boolean
): Promise<void> {
this.code[documentUri] = code;
this.languageService.update_document(documentUri, version, code);
this.languageService.update_document(documentUri, version, code, isExe);
}

async closeDocument(documentUri: string): Promise<void> {
Expand Down
6 changes: 4 additions & 2 deletions npm/test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ test("language service diagnostics", async () => {
let m1 = M(q1);
return [m1];
}
}`
}`,
true // PackageType "exe"
);
assert(gotDiagnostics);
});
Expand All @@ -423,7 +424,8 @@ test("language service diagnostics - web worker", async () => {
let m1 = M(q1);
return [m1];
}
}`
}`,
true // PackageType "exe"
);
languageService.terminate();
assert(gotDiagnostics);
Expand Down
3 changes: 2 additions & 1 deletion playground/src/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ export function Editor(props: {
await props.languageService.updateDocument(
srcModel.uri.toString(),
srcModel.getVersionId(),
srcModel.getValue()
srcModel.getValue(),
!props.kataExercise // Kata exercises are always libraries
);
const measure = performance.measure(
"update-document",
Expand Down
3 changes: 2 additions & 1 deletion vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ function registerDocumentUpdateHandlers(languageService: ILanguageService) {
languageService.updateDocument(
document.uri.toString(),
document.version,
document.getText()
document.getText(),
true // PackageType "exe"
);
}
}
Expand Down
15 changes: 12 additions & 3 deletions wasm/src/language_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

use miette::{Diagnostic, Severity};
use qsc::compile;
use qsc::{self, compile};
use serde::{Deserialize, Serialize};
use std::{fmt::Write, iter};
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -32,8 +32,17 @@ impl LanguageService {
LanguageService(inner)
}

pub fn update_document(&mut self, uri: &str, version: u32, text: &str) {
self.0.update_document(uri, version, text);
pub fn update_document(&mut self, uri: &str, version: u32, text: &str, is_exe: bool) {
self.0.update_document(
uri,
version,
text,
if is_exe {
qsc::PackageType::Exe
} else {
qsc::PackageType::Lib
},
);
}

pub fn close_document(&mut self, uri: &str) {
Expand Down

0 comments on commit be9b870

Please sign in to comment.