Skip to content

Commit

Permalink
Merge pull request #69 from wkillerud/show
Browse files Browse the repository at this point in the history
Show
  • Loading branch information
wkillerud authored Nov 18, 2023
2 parents 0a7c2c6 + c7a85af commit 2c1e521
Show file tree
Hide file tree
Showing 30 changed files with 166 additions and 7 deletions.
1 change: 1 addition & 0 deletions fixtures/e2e/namespace/_index.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@forward "./functions" as fun-*;
@forward "./mixins" as mix-* hide secret, other-secret;
@forward "./variables" hide $secret;
@forward "./show" show $one;
2 changes: 2 additions & 0 deletions fixtures/e2e/namespace/_show.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$one: blue;
$two: red;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward "./base" hide $color-white;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$color-black: black;
$color-grey: grey;
$color-white: white;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward "./base" hide $color-black;
3 changes: 3 additions & 0 deletions fixtures/unit/completion/multi-level-hide/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@use "./colors";

$text-color: colors.|
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@forward "./branch-a" hide $color-white;
@forward "./branch-b";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$color-white: white;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward "./base";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$color-white: white;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward "./base";
3 changes: 3 additions & 0 deletions fixtures/unit/completion/same-symbol-name-hide/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@use "./colors";

$text-color: colors.|
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@forward "./branch-a" show $color-black;
@forward "./branch-b";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$color-black: black;
$color-grey: grey;
$color-white: white;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward "./base";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$color-black: black;
$color-grey: grey;
$color-white: white;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward "./base";
3 changes: 3 additions & 0 deletions fixtures/unit/completion/same-symbol-name-show/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@use "./colors";

$text-color: colors.|
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "some-sass",
"displayName": "Some Sass: extended support for SCSS and SassDoc",
"description": "Full support for @use and @forward, including aliases, prefixes and hiding. Rich documentation through SassDoc. Workspace-wide code navigation and refactoring.",
"version": "2.14.2",
"version": "2.14.3",
"publisher": "SomewhatStationery",
"license": "MIT",
"engines": {
Expand Down
15 changes: 14 additions & 1 deletion server/src/features/completion/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ function traverseTree(
accumulator: Map<string, CompletionItem[]>,
leaf: IScssDocument,
hiddenSymbols: string[] = [],
shownSymbols: string[] = [],
accumulatedPrefix = "",
) {
if (accumulator.has(leaf.uri)) {
Expand All @@ -263,6 +264,7 @@ function traverseTree(
document,
context,
hiddenSymbols,
shownSymbols,
accumulatedPrefix,
);
completionItems = completionItems.concat(variables);
Expand All @@ -274,6 +276,7 @@ function traverseTree(
document,
context,
hiddenSymbols,
shownSymbols,
accumulatedPrefix,
);
completionItems = completionItems.concat(mixins);
Expand All @@ -285,6 +288,7 @@ function traverseTree(
document,
context,
hiddenSymbols,
shownSymbols,
accumulatedPrefix,
);
completionItems = completionItems.concat(functions);
Expand All @@ -294,6 +298,7 @@ function traverseTree(
const placeholders = createPlaceholderCompletionItems(
scssDocument,
hiddenSymbols,
shownSymbols,
);
completionItems = completionItems.concat(placeholders);
}
Expand All @@ -314,11 +319,18 @@ function traverseTree(
}

let hidden = hiddenSymbols;
let shown = shownSymbols;
if (
(child as ScssForward).hide &&
(child as ScssForward).hide.length > 0
) {
hidden = hiddenSymbols.concat((child as ScssForward).hide);
hidden = hidden.concat((child as ScssForward).hide);
}
if (
(child as ScssForward).show &&
(child as ScssForward).show.length > 0
) {
shown = shown.concat((child as ScssForward).show);
}

let prefix = accumulatedPrefix;
Expand All @@ -332,6 +344,7 @@ function traverseTree(
accumulator,
childDocument,
hidden,
shown,
prefix,
);
}
Expand Down
5 changes: 5 additions & 0 deletions server/src/features/completion/function-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function createFunctionCompletionItems(
currentDocument: TextDocument,
context: CompletionContext,
hiddenSymbols: string[] = [],
shownSymbols: string[] = [],
prefix = "",
): CompletionItem[] {
const completions: CompletionItem[] = [];
Expand All @@ -39,6 +40,10 @@ export function createFunctionCompletionItems(
continue;
}

if (shownSymbols.length > 0 && !shownSymbols.includes(func.name)) {
continue;
}

// Client needs the namespace as part of the text that is matched,
// and inserted text needs to include the `.` which will otherwise
// be replaced (except when we're embedded in Vue, Svelte or Astro).
Expand Down
5 changes: 5 additions & 0 deletions server/src/features/completion/mixin-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function createMixinCompletionItems(
currentDocument: TextDocument,
context: CompletionContext,
hiddenSymbols: string[] = [],
shownSymbols: string[] = [],
prefix = "",
): CompletionItem[] {
const completions: CompletionItem[] = [];
Expand All @@ -40,6 +41,10 @@ export function createMixinCompletionItems(
continue;
}

if (shownSymbols.length > 0 && !shownSymbols.includes(mixin.name)) {
continue;
}

const documentation = makeMixinDocumentation(mixin, scssDocument);

// Client needs the namespace as part of the text that is matched,
Expand Down
5 changes: 5 additions & 0 deletions server/src/features/completion/placeholder-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { applySassDoc } from "../../utils/sassdoc";
export function createPlaceholderCompletionItems(
scssDocument: IScssDocument,
hiddenSymbols: string[] = [],
shownSymbols: string[] = [],
): CompletionItem[] {
const completions: CompletionItem[] = [];

Expand All @@ -19,6 +20,10 @@ export function createPlaceholderCompletionItems(
continue;
}

if (shownSymbols.length > 0 && !shownSymbols.includes(placeholder.name)) {
continue;
}

const label = placeholder.name;
const filterText = placeholder.name.substring(1);

Expand Down
5 changes: 5 additions & 0 deletions server/src/features/completion/variable-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function createVariableCompletionItems(
currentDocument: TextDocument,
context: CompletionContext,
hiddenSymbols: string[] = [],
shownSymbols: string[] = [],
prefix = "",
): CompletionItem[] {
const completions: CompletionItem[] = [];
Expand Down Expand Up @@ -65,6 +66,10 @@ export function createVariableCompletionItems(
continue;
}

if (shownSymbols.length > 0 && !shownSymbols.includes(variable.name)) {
continue;
}

if (isPrivate) {
sortText = label.replace(/^$[_-]/, "");
}
Expand Down
5 changes: 4 additions & 1 deletion server/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { IScssSymbols } from "./scss-symbol";
export const reModuleAtRule = /@(?:use|forward|import)/;
export const reUse = /@use ["'|](?<url>.+)["'|](?: as (?<namespace>\*|\w+))?;/;
export const reForward =
/@forward ["'|](?<url>.+)["'|](?: as (?<prefix>\w+-)\*)?(?: hide (?<hide>.+))?;/;
/@forward ["'|](?<url>.+)["'|](?: as (?<prefix>\w+-)\*)?(?: hide (?<hide>.+))?(?: show (?<show>.+))?;/;
export const reImport = /@import ["'|](?<url>.+)["'|]/;
export const rePlaceholder = /^\s*%(?<name>\w+)/;
export const rePlaceholderUsage = /\s*@extend\s+(?<name>%[\w\d-_]+)/;
Expand Down Expand Up @@ -152,6 +152,9 @@ async function findDocumentSymbols(
hide: matchForward.groups?.["hide"]
? matchForward.groups["hide"].split(",").map((s) => s.trim())
: [],
show: matchForward.groups?.["show"]
? matchForward.groups["show"].split(",").map((s) => s.trim())
: [],
});
}

Expand Down
1 change: 1 addition & 0 deletions server/src/parser/scss-symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface ScssUse extends ScssLink {

export interface ScssForward extends ScssLink {
hide: string[];
show: string[];
prefix?: string;
}

Expand Down
89 changes: 89 additions & 0 deletions server/src/test/features/completion-visibility.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as assert from "assert";
import { changeConfiguration, useContext } from "../../context-provider";
import { doCompletion } from "../../features/completion";
import { NodeFileSystem } from "../../node-file-system";
import { IScssDocument } from "../../parser";
import ScannerService from "../../scanner";
import { getUri } from "../fixture-helper";
import * as helpers from "../helpers";

describe("Providers/Completion", () => {
beforeEach(async () => {
helpers.createTestContext(new NodeFileSystem());

const settings = helpers.makeSettings({
suggestFromUseOnly: true,
});
changeConfiguration(settings);
});

describe("Hide", () => {
it("supports multi-level hiding", async () => {
const workspaceUri = getUri("completion/multi-level-hide/");
const docUri = getUri("completion/multi-level-hide/styles.scss");
const scanner = new ScannerService();
await scanner.scan([docUri], workspaceUri);
const { storage } = useContext();
const stylesDoc = storage.get(docUri) as IScssDocument;

const completions = await doCompletion(
stylesDoc,
stylesDoc.getText().indexOf("|"),
);

// $color-black and $color-white are hidden at different points

assert.equal(
completions.items.length,
1,
"Expected only one suggestion from the multi-level-hide fixture",
);
assert.equal(completions.items[0].label, "$color-grey");
});

it("doesn't hide symbol with same name in different part of dependency graph", async () => {
const workspaceUri = getUri("completion/same-symbol-name-hide/");
const docUri = getUri("completion/same-symbol-name-hide/styles.scss");
const scanner = new ScannerService();
await scanner.scan([docUri], workspaceUri);
const { storage } = useContext();
const stylesDoc = storage.get(docUri) as IScssDocument;

const completions = await doCompletion(
stylesDoc,
stylesDoc.getText().indexOf("|"),
);

// $color-white is hidden in branch-a, but not in branch-b
assert.equal(
completions.items.length,
1,
"Expected a suggestion from the same-symbol-name-hide fixture",
);
assert.equal(completions.items[0].label, "$color-white");
});
});

describe("Show", () => {
it("doesn't show symbol with same name in different part of dependency graph", async () => {
const workspaceUri = getUri("completion/same-symbol-name-show/");
const docUri = getUri("completion/same-symbol-name-show/styles.scss");
const scanner = new ScannerService();
await scanner.scan([docUri], workspaceUri);
const { storage } = useContext();
const stylesDoc = storage.get(docUri) as IScssDocument;

const completions = await doCompletion(
stylesDoc,
stylesDoc.getText().indexOf("|"),
);

// One branch only shows $color-black, but the other has three symbols including another $color-black
assert.equal(
completions.items.length,
4,
"Expected four suggestions from the same-symbol-name-show fixture",
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from "path";
import { URI } from "vscode-uri";

function getDocPath(p: string) {
return path.resolve(__dirname, "../../../../fixtures/unit", p);
return path.resolve(__dirname, "../../../fixtures/unit", p);
}

export function getUri(p: string) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/test/scanner/scanner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { isMatch } from "micromatch";
import { useContext } from "../../context-provider";
import { NodeFileSystem } from "../../node-file-system";
import ScannerService from "../../scanner";
import { getUri } from "../fixture-helper";
import * as helpers from "../helpers";
import { getUri } from "./scanner-helper";

describe("Services/Scanner", () => {
beforeEach(() => {
Expand Down

0 comments on commit 2c1e521

Please sign in to comment.