Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/VSCodeExtension/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function createNewProject(context: vscode.ExtensionContext) {

export function installTemplates(dotNetSdk: DotnetInfo, packageInfo?: IPackageInfo) {
let packageVersion =
oc(packageInfo).nugetVersion
oc(packageInfo).nugetVersion()
? `::${packageInfo!.nugetVersion}`
: "";
let proc = cp.spawn(
Expand Down
2 changes: 1 addition & 1 deletion src/VSCodeExtension/src/formatter/format-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as vscode from "vscode";
import { FormatRule, formatter } from "./formatter";
import indentRules from "./rules/indent";
import operationRules from "./rules/operation";
import operationRules from "./rules/declaration";
import controlStructureRules from "./rules/control-structure";

const rules: FormatRule[] = [
Expand Down
4 changes: 1 addition & 3 deletions src/VSCodeExtension/src/formatter/rules/control-structure.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { FormatRule } from "../formatter";

/**
* Laves just one space after an if statement
* Leaves just one space after an if statement.
*
* @param code input string
*
* @example `if (1 == 1){H(qs[0]);}` ->
* `if (1 == 1){H(qs[0]);}`
*
* more examples in the unit tests
*/
export const spaceAfterIf: FormatRule = (code: string): string => {
return code.replace(/(^| +|;|})if[ \n]*/, "$1if ");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* Formats operation declarations.
*
* @param code incoming code
*
* @example `operation Foo(q:Qubit, n :Int) : Bool` ->
* `operation Foo (q : Qubit, n : Int) : Bool`
*/
export const argsRule = (code: string): string => {
const operationMatcher: RegExp = /operation\s*(\w+)\s*(\(.*\))\s*:\s*(\w+)/g;
const firstArgMatcher: RegExp = /\(((\w*)\s*:\s*([a-zA-Z]+))/g
const restArgMatcher: RegExp = /,\s*((\w*)\s*:\s*([a-zA-Z]+))/g;
return code.replace(operationMatcher, (match, opName, args, retType) => {
args = args
.replace(
firstArgMatcher,
(match: string, group: string, variable: string, type: string) =>
`(${variable} : ${type}`
)
.replace(
restArgMatcher,
(match: string, group: string, variable: string, type: string) =>
`, ${variable} : ${type}`
);
return `operation ${opName} ${args} : ${retType}`;
});
};
export default [argsRule];
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

/**
* Formats operation declarations.
*
* @param code incoming code
*
* @example `operation Foo(q:Qubit, n :Int) : Bool` ->
* `operation Foo (q : Qubit, n : Int) : Bool`
*/
export const argsRule = (code: string): string => {
const declarationMatcher: RegExp = /(operation|function)\s*(\w+)\s*(\(.*\))\s*:\s*(\w+)/g;
const firstArgMatcher: RegExp = /\(((\w*)\s*:\s*([a-zA-Z]+))/g
const restArgMatcher: RegExp = /,\s*((\w*)\s*:\s*([a-zA-Z]+))/g;

return code.replace(declarationMatcher, (match, keyword, opName, args, retType) => {
args = args
.replace(
firstArgMatcher,
(match: string, group: string, variable: string, type: string) =>
`(${variable} : ${type}`
)
.replace(
restArgMatcher,
(match: string, group: string, variable: string, type: string) =>
`, ${variable} : ${type}`
);
return `${keyword} ${opName} ${args} : ${retType}`;
});
};

export default [argsRule];
2 changes: 1 addition & 1 deletion src/VSCodeExtension/src/formatter/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { FormatRule } from "../formatter";
* `namespace Foo {}`
*/
export const namespaceRule: FormatRule = (code: string) => {
const namespaceMatcher: RegExp = /^\s*namespace\s*(\w+)\s*(\S|\S.*\S)\s*$/g;
const namespaceMatcher: RegExp = /^\s*namespace\s+(\w+(?:\.\w+)*)\s*({|{.*})\s*$/g;

return code.replace(namespaceMatcher, (match, namespace, rest) => `namespace ${namespace} ${rest}`);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import "mocha";
import * as assert from "assert";
import { formatter } from "../../formatter/formatter";
import { argsRule } from "../../formatter/rules/operation";
import { argsRule } from "../../formatter/rules/declaration";

describe("arguments rule", () => {
it("no error", () => {
it("has no error", () => {
const code = "operation Foo (q : Qubit, n : Int) : Bool";
const expectedCode = "operation Foo (q : Qubit, n : Int) : Bool";

assert.equal(formatter(code, [argsRule]), expectedCode);
});

it("keep leading whitespace", () => {
it("keeps leading whitespace", () => {
const code = " operation Foo (q : Qubit, n : Int) : Bool";
const expectedCode = " operation Foo (q : Qubit, n : Int) : Bool";

assert.equal(formatter(code, [argsRule]), expectedCode);
});

it("remove unnecessary spaces (non-arguments)", () => {
it("removes unnecessary spaces (non-arguments)", () => {
const expectedCode = "operation Foo (q : Qubit, n : Int) : Bool";

let code = "operation Foo (q : Qubit, n : Int) : Bool";
Expand All @@ -34,7 +34,7 @@ describe("arguments rule", () => {
assert.equal(formatter(code, [argsRule]), expectedCode);
});

it("remove unnecessary spaces (arguments)", () => {
it("removes unnecessary spaces (arguments)", () => {
const expectedCode = "operation Foo (q : Qubit, n : Int) : Bool";

let code = "operation Foo (q : Qubit, n : Int) : Bool";
Expand All @@ -52,4 +52,11 @@ describe("arguments rule", () => {
code = "operation Foo (q : Qubit, n : Int) : Bool";
assert.equal(formatter(code, [argsRule]), expectedCode);
});

it("matches functions", () => {
const code = " function Foo (q :Qubit, n : Int) : Bool";
const expectedCode = " function Foo (q : Qubit, n : Int) : Bool";

assert.equal(formatter(code, [argsRule]), expectedCode);
});
});
2 changes: 1 addition & 1 deletion src/VSCodeExtension/src/tests/unit/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as assert from "assert";
import { formatter } from "../../formatter/formatter";

describe("formatter tests", () => {
it("with no rules, the code is unchanged", () => {
it("has no rules", () => {
const code = "if(1 == 1){H(qs[0]);}";
const expectedCode = "if(1 == 1){H(qs[0]);}";

Expand Down
25 changes: 22 additions & 3 deletions src/VSCodeExtension/src/tests/unit/indent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { formatter } from "../../formatter/formatter";
import { namespaceRule } from "../../formatter/rules/indent";

describe("namespace rule", () => {
it("no error", () => {
it("has no error", () => {
const code = "namespace Foo {";
const expectedCode = "namespace Foo {";

assert.equal(formatter(code, []), expectedCode);
});

it("trim whitespace", () => {
it("trims whitespace", () => {
const expectedCode = "namespace Foo {";

let code = " namespace Foo {";
Expand All @@ -30,7 +30,7 @@ describe("namespace rule", () => {
assert.equal(formatter(code, [namespaceRule]), expectedCode);
});

it("match lines with inline body", () => {
it("matches lines with inline body", () => {
const expectedCode = "namespace Foo { // ... }";

let code = " namespace Foo { // ... }";
Expand All @@ -48,4 +48,23 @@ describe("namespace rule", () => {
code = " namespace Foo { // ... } ";
assert.equal(formatter(code, [namespaceRule]), expectedCode);
});

it("matches dot-separated names", () => {
const expectedCode = "namespace Microsoft.Quantum.Arrays { // ... }";

let code = " namespace Microsoft.Quantum.Arrays { // ... }";
assert.equal(formatter(code, [namespaceRule]), expectedCode);

code = "namespace Microsoft.Quantum.Arrays { // ... } ";
assert.equal(formatter(code, [namespaceRule]), expectedCode);

code = "namespace Microsoft.Quantum.Arrays { // ... } ";
assert.equal(formatter(code, [namespaceRule]), expectedCode);

code = "namespace Microsoft.Quantum.Arrays { // ... } ";
assert.equal(formatter(code, [namespaceRule]), expectedCode);

code = " namespace Microsoft.Quantum.Arrays { // ... } ";
assert.equal(formatter(code, [namespaceRule]), expectedCode);
});
});