qsharp-lang 1.0.22-rc → 1.0.24-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ export interface ICompiler {
7
7
  getHir(code: string): Promise<string>;
8
8
  run(code: string, expr: string, shots: number, eventHandler: IQscEventTarget): Promise<void>;
9
9
  getQir(code: string): Promise<string>;
10
+ getEstimates(code: string, params: string): Promise<string>;
10
11
  checkExerciseSolution(user_code: string, exercise_sources: string[], eventHandler: IQscEventTarget): Promise<boolean>;
11
12
  }
12
13
  export type ICompilerWorker = ICompiler & IServiceProxy;
@@ -16,6 +17,7 @@ export declare class Compiler implements ICompiler {
16
17
  constructor(wasm: Wasm);
17
18
  checkCode(code: string): Promise<VSDiagnostic[]>;
18
19
  getQir(code: string): Promise<string>;
20
+ getEstimates(code: string, params: string): Promise<string>;
19
21
  getHir(code: string): Promise<string>;
20
22
  run(code: string, expr: string, shots: number, eventHandler: IQscEventTarget): Promise<void>;
21
23
  checkExerciseSolution(user_code: string, exercise_sources: string[], eventHandler: IQscEventTarget): Promise<boolean>;
@@ -21,6 +21,9 @@ export class Compiler {
21
21
  async getQir(code) {
22
22
  return this.wasm.get_qir(code);
23
23
  }
24
+ async getEstimates(code, params) {
25
+ return this.wasm.get_estimates(code, params);
26
+ }
24
27
  async getHir(code) {
25
28
  return this.wasm.get_hir(code);
26
29
  }
@@ -29,7 +29,6 @@ export declare class QscEventTarget implements IQscEventTarget {
29
29
  private results;
30
30
  private shotActive;
31
31
  private animationFrameId;
32
- private supportsUiRefresh;
33
32
  addEventListener<T extends QscUiEvents["type"]>(type: T, listener: (event: Extract<QscEvents, {
34
33
  type: T;
35
34
  }>) => void): void;
@@ -31,9 +31,6 @@ export class QscEventTarget {
31
31
  this.results = [];
32
32
  this.shotActive = false;
33
33
  this.animationFrameId = 0;
34
- this.supportsUiRefresh = false;
35
- this.supportsUiRefresh =
36
- typeof globalThis.requestAnimationFrame === "function";
37
34
  if (captureEvents) {
38
35
  this.addEventListener("Message", (ev) => this.onMessage(ev.detail));
39
36
  this.addEventListener("DumpMachine", (ev) => this.onDumpMachine(ev.detail));
@@ -67,10 +64,10 @@ export class QscEventTarget {
67
64
  }
68
65
  }
69
66
  queueUiRefresh() {
70
- if (this.supportsUiRefresh && !this.animationFrameId) {
71
- this.animationFrameId = requestAnimationFrame(() => {
67
+ if (!this.animationFrameId) {
68
+ this.animationFrameId = setTimeout(() => {
72
69
  this.onUiRefresh();
73
- });
70
+ }, 50); // 20 fps is plenty for the rendering we do
74
71
  }
75
72
  }
76
73
  onUiRefresh() {
@@ -5,6 +5,7 @@ const requests = {
5
5
  checkCode: "request",
6
6
  getHir: "request",
7
7
  getQir: "request",
8
+ getEstimates: "request",
8
9
  run: "requestWithProgress",
9
10
  checkExerciseSolution: "requestWithProgress",
10
11
  };
@@ -3,7 +3,7 @@ import { IQscEventTarget } from "../compiler/events.js";
3
3
  import { IServiceProxy } from "../worker-proxy.js";
4
4
  type QscWasm = typeof import("../../lib/node/qsc_wasm.cjs");
5
5
  export interface IDebugService {
6
- loadSource(path: string, source: string, entry: string | undefined): Promise<string>;
6
+ loadSource(path: string, source: string, target: "base" | "full", entry: string | undefined): Promise<string>;
7
7
  getBreakpoints(path: string): Promise<IBreakpointSpan[]>;
8
8
  getLocalVariables(): Promise<Array<IVariable>>;
9
9
  captureQuantumState(): Promise<Array<IQuantumState>>;
@@ -20,7 +20,7 @@ export declare class QSharpDebugService implements IDebugService {
20
20
  private debugService;
21
21
  private code;
22
22
  constructor(wasm: QscWasm);
23
- loadSource(path: string, source: string, entry: string | undefined): Promise<string>;
23
+ loadSource(path: string, source: string, target: "base" | "full", entry: string | undefined): Promise<string>;
24
24
  getStackFrames(): Promise<IStackFrame[]>;
25
25
  evalNext(bps: number[], eventHandler: IQscEventTarget): Promise<IStructStepResult>;
26
26
  evalStepIn(bps: number[], eventHandler: IQscEventTarget): Promise<IStructStepResult>;
@@ -12,9 +12,9 @@ export class QSharpDebugService {
12
12
  this.wasm = wasm;
13
13
  this.debugService = new wasm.DebugService();
14
14
  }
15
- async loadSource(path, source, entry) {
15
+ async loadSource(path, source, target, entry) {
16
16
  this.code[path] = source;
17
- return this.debugService.load_source(path, source, entry);
17
+ return this.debugService.load_source(path, source, target, entry);
18
18
  }
19
19
  async getStackFrames() {
20
20
  const stack_frame_list = this.debugService.get_stack_frames();
@@ -13,7 +13,13 @@ export type LanguageServiceEvent = {
13
13
  export interface ILanguageService {
14
14
  updateConfiguration(config: IWorkspaceConfiguration): Promise<void>;
15
15
  updateDocument(uri: string, version: number, code: string): Promise<void>;
16
+ updateNotebookDocument(notebookUri: string, version: number, cells: {
17
+ uri: string;
18
+ version: number;
19
+ code: string;
20
+ }[]): Promise<void>;
16
21
  closeDocument(uri: string): Promise<void>;
22
+ closeNotebookDocument(notebookUri: string, cellUris: string[]): Promise<void>;
17
23
  getCompletions(documentUri: string, offset: number): Promise<ICompletionList>;
18
24
  getHover(documentUri: string, offset: number): Promise<IHover | undefined>;
19
25
  getDefinition(documentUri: string, offset: number): Promise<ILocation | undefined>;
@@ -38,7 +44,13 @@ export declare class QSharpLanguageService implements ILanguageService {
38
44
  constructor(wasm: QscWasm);
39
45
  updateConfiguration(config: IWorkspaceConfiguration): Promise<void>;
40
46
  updateDocument(documentUri: string, version: number, code: string): Promise<void>;
47
+ updateNotebookDocument(notebookUri: string, version: number, cells: {
48
+ uri: string;
49
+ version: number;
50
+ code: string;
51
+ }[]): Promise<void>;
41
52
  closeDocument(documentUri: string): Promise<void>;
53
+ closeNotebookDocument(documentUri: string, cellUris: string[]): Promise<void>;
42
54
  getCompletions(documentUri: string, offset: number): Promise<ICompletionList>;
43
55
  getHover(documentUri: string, offset: number): Promise<IHover | undefined>;
44
56
  getDefinition(documentUri: string, offset: number): Promise<ILocation | undefined>;
@@ -53,6 +65,6 @@ export declare class QSharpLanguageService implements ILanguageService {
53
65
  removeEventListener<T extends LanguageServiceEvent["type"]>(type: T, listener: (event: Extract<LanguageServiceEvent, {
54
66
  type: T;
55
67
  }>) => void): void;
56
- onDiagnostics(uri: string, version: number, diagnostics: VSDiagnostic[]): void;
68
+ onDiagnostics(uri: string, version: number | undefined, diagnostics: VSDiagnostic[]): void;
57
69
  }
58
70
  export {};
@@ -19,10 +19,23 @@ export class QSharpLanguageService {
19
19
  this.code[documentUri] = code;
20
20
  this.languageService.update_document(documentUri, version, code);
21
21
  }
22
+ async updateNotebookDocument(notebookUri, version, cells) {
23
+ // Note: If a cell was deleted, its uri & contents will remain in the map.
24
+ // This is harmless and it keeps the code simpler to just leave it this way
25
+ // instead of trying to maintain a perfect map.
26
+ for (const cell of cells) {
27
+ this.code[cell.uri] = cell.code;
28
+ }
29
+ this.languageService.update_notebook_document(notebookUri, cells);
30
+ }
22
31
  async closeDocument(documentUri) {
23
32
  delete this.code[documentUri];
24
33
  this.languageService.close_document(documentUri);
25
34
  }
35
+ async closeNotebookDocument(documentUri, cellUris) {
36
+ cellUris.forEach((uri) => delete this.code[uri]);
37
+ this.languageService.close_notebook_document(documentUri, cellUris);
38
+ }
26
39
  async getCompletions(documentUri, offset) {
27
40
  const code = this.code[documentUri];
28
41
  if (code === undefined) {
@@ -32,9 +45,7 @@ export class QSharpLanguageService {
32
45
  const convertedOffset = mapUtf16UnitsToUtf8Units([offset], code)[offset];
33
46
  const result = this.languageService.get_completions(documentUri, convertedOffset);
34
47
  result.items.forEach((item) => item.additionalTextEdits?.forEach((edit) => {
35
- const mappedSpan = mapUtf8UnitsToUtf16Units([edit.range.start, edit.range.end], code);
36
- edit.range.start = mappedSpan[edit.range.start];
37
- edit.range.end = mappedSpan[edit.range.end];
48
+ updateSpanFromUtf8ToUtf16(edit.range, code);
38
49
  }));
39
50
  return result;
40
51
  }
@@ -47,35 +58,40 @@ export class QSharpLanguageService {
47
58
  const convertedOffset = mapUtf16UnitsToUtf8Units([offset], code)[offset];
48
59
  const result = this.languageService.get_hover(documentUri, convertedOffset);
49
60
  if (result) {
50
- const mappedSpan = mapUtf8UnitsToUtf16Units([result.span.start, result.span.end], code);
51
- result.span.start = mappedSpan[result.span.start];
52
- result.span.end = mappedSpan[result.span.end];
61
+ updateSpanFromUtf8ToUtf16(result.span, code);
53
62
  }
54
63
  return result;
55
64
  }
56
65
  async getDefinition(documentUri, offset) {
57
- let code = this.code[documentUri];
58
- if (code === undefined) {
66
+ const sourceCode = this.code[documentUri];
67
+ if (sourceCode === undefined) {
59
68
  log.error(`getDefinition: expected ${documentUri} to be in the document map`);
60
69
  return undefined;
61
70
  }
62
- const convertedOffset = mapUtf16UnitsToUtf8Units([offset], code)[offset];
71
+ const convertedOffset = mapUtf16UnitsToUtf8Units([offset], sourceCode)[offset];
63
72
  const result = this.languageService.get_definition(documentUri, convertedOffset);
64
73
  if (result) {
65
- // Inspect the URL protocol (equivalent to the URI scheme + ":").
66
- // If the scheme is our library scheme, we need to call the wasm to
67
- // provide the library file's contents to do the utf8->utf16 mapping.
68
- const url = new URL(result.source);
69
- if (url.protocol === qsharpLibraryUriScheme + ":") {
70
- code = wasm.get_library_source_content(url.pathname);
71
- if (code === undefined) {
72
- log.error(`getDefinition: expected ${url} to be in the library`);
73
- return undefined;
74
+ let targetCode = this.code[result.source];
75
+ if (targetCode === undefined) {
76
+ // Inspect the URL protocol (equivalent to the URI scheme + ":").
77
+ // If the scheme is our library scheme, we need to call the wasm to
78
+ // provide the library file's contents to do the utf8->utf16 mapping.
79
+ const url = new URL(result.source);
80
+ if (url.protocol === qsharpLibraryUriScheme + ":") {
81
+ targetCode = wasm.get_library_source_content(url.pathname);
82
+ if (targetCode === undefined) {
83
+ log.error(`getDefinition: expected ${url} to be in the library`);
84
+ return undefined;
85
+ }
74
86
  }
75
87
  }
76
- const mappedSpan = mapUtf8UnitsToUtf16Units([result.span.start, result.span.end], code);
77
- result.span.start = mappedSpan[result.span.start];
78
- result.span.end = mappedSpan[result.span.end];
88
+ if (targetCode) {
89
+ updateSpanFromUtf8ToUtf16(result.span, targetCode);
90
+ }
91
+ else {
92
+ // https://github.com/microsoft/qsharp/issues/851
93
+ log.error(`cannot do utf8->utf16 mapping for ${result.source} since contents are not available`);
94
+ }
79
95
  }
80
96
  return result;
81
97
  }
@@ -102,12 +118,11 @@ export class QSharpLanguageService {
102
118
  }
103
119
  }
104
120
  if (resultCode) {
105
- const mappedSpan = mapUtf8UnitsToUtf16Units([result.span.start, result.span.end], resultCode);
106
- result.span.start = mappedSpan[result.span.start];
107
- result.span.end = mappedSpan[result.span.end];
121
+ updateSpanFromUtf8ToUtf16(result.span, resultCode);
108
122
  references.push(result);
109
123
  }
110
124
  else {
125
+ // https://github.com/microsoft/qsharp/issues/851
111
126
  log.error(`cannot do utf8->utf16 mapping for ${result.source} since contents are not available`);
112
127
  }
113
128
  }
@@ -128,9 +143,7 @@ export class QSharpLanguageService {
128
143
  if (result) {
129
144
  result.signatures = result.signatures.map((sig) => {
130
145
  sig.parameters = sig.parameters.map((param) => {
131
- const mappedSpan = mapUtf8UnitsToUtf16Units([param.label.start, param.label.end], sig.label);
132
- param.label.start = mappedSpan[param.label.start];
133
- param.label.end = mappedSpan[param.label.end];
146
+ updateSpanFromUtf8ToUtf16(param.label, sig.label);
134
147
  return param;
135
148
  });
136
149
  return sig;
@@ -151,9 +164,7 @@ export class QSharpLanguageService {
151
164
  const code = this.code[uri];
152
165
  if (code) {
153
166
  const mappedEdits = edits.map((edit) => {
154
- const mappedSpan = mapUtf8UnitsToUtf16Units([edit.range.start, edit.range.end], code);
155
- edit.range.start = mappedSpan[edit.range.start];
156
- edit.range.end = mappedSpan[edit.range.end];
167
+ updateSpanFromUtf8ToUtf16(edit.range, code);
157
168
  return edit;
158
169
  });
159
170
  mappedChanges.push([uri, mappedEdits]);
@@ -171,9 +182,7 @@ export class QSharpLanguageService {
171
182
  const convertedOffset = mapUtf16UnitsToUtf8Units([offset], code)[offset];
172
183
  const result = this.languageService.prepare_rename(documentUri, convertedOffset);
173
184
  if (result) {
174
- const mappedSpan = mapUtf8UnitsToUtf16Units([result.range.start, result.range.end], code);
175
- result.range.start = mappedSpan[result.range.start];
176
- result.range.end = mappedSpan[result.range.end];
185
+ updateSpanFromUtf8ToUtf16(result.range, code);
177
186
  }
178
187
  return result;
179
188
  }
@@ -203,7 +212,7 @@ export class QSharpLanguageService {
203
212
  const event = new Event("diagnostics");
204
213
  event.detail = {
205
214
  uri,
206
- version,
215
+ version: version ?? 0,
207
216
  diagnostics: empty ? [] : mapDiagnostics(diagnostics, code),
208
217
  };
209
218
  this.eventHandler.dispatchEvent(event);
@@ -213,3 +222,8 @@ export class QSharpLanguageService {
213
222
  }
214
223
  }
215
224
  }
225
+ function updateSpanFromUtf8ToUtf16(span, code) {
226
+ const mappedSpan = mapUtf8UnitsToUtf16Units([span.start, span.end], code);
227
+ span.start = mappedSpan[span.start];
228
+ span.end = mappedSpan[span.end];
229
+ }
@@ -4,7 +4,9 @@ import { createDispatcher, createProxy, } from "../worker-proxy.js";
4
4
  const requests = {
5
5
  updateConfiguration: "request",
6
6
  updateDocument: "request",
7
+ updateNotebookDocument: "request",
7
8
  closeDocument: "request",
9
+ closeNotebookDocument: "request",
8
10
  getCompletions: "request",
9
11
  getHover: "request",
10
12
  getDefinition: "request",