Skip to content

Commit

Permalink
Merge pull request #18 from wkillerud/fix/patch-sunday
Browse files Browse the repository at this point in the history
Fix/patch sunday
  • Loading branch information
wkillerud authored Aug 14, 2022
2 parents b3d7360 + affbda6 commit 055664a
Show file tree
Hide file tree
Showing 26 changed files with 185 additions and 52 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Visit the [release section on GitHub](https://github.com/wkillerud/vscode-scss/r

Search for Some Sass (`SomewhatStationery.some-sass`) from the extension installer within VS Code or install from [the Marketplace](https://marketplace.visualstudio.com/items?itemName=SomewhatStationery.some-sass).

If you have SCSS IntelliSense (`mrmlnc.vscode-scss`) installed you should disable or uninstall it. Otherwise the two extensions will both provide hover information and code suggestions.
See [Recommended settings](#recommended-settings-for-visual-studio-code) for some tips on how to tweak code suggestions to your liking.

Note that if you have SCSS IntelliSense (`mrmlnc.vscode-scss`) installed you should disable or uninstall it. Otherwise the two extensions will both provide hover information and code suggestions.

## Usage

Expand Down Expand Up @@ -166,7 +168,6 @@ Depending on your project size, you may want to tweak this setting to control ho
- JSON key: `somesass.scannerDepth`.
- Default: `30`.


#### Stop scanner from following links

`@deprecated`
Expand All @@ -178,7 +179,6 @@ after `@import` becomes CSS-only.
- JSON key: `somesass.scanImportedFiles`.
- Default: `true`.


## What this extension does _not_ do

- Formating. Consider using [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) if you want automatic formating.
Expand Down
5 changes: 5 additions & 0 deletions fixtures/e2e/completion/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ $fonts: -apple-system;
color: ns.
@include ns.
--runtime-var: var(--other-var, #{ns.})
font-size: -#{ns.}
}

@function _multiply($value) {
@return $value * ns.;
}
17 changes: 0 additions & 17 deletions fixtures/unit/entry.scss

This file was deleted.

3 changes: 0 additions & 3 deletions fixtures/unit/functions/one.scss

This file was deleted.

3 changes: 0 additions & 3 deletions fixtures/unit/functions/two.scss

This file was deleted.

3 changes: 0 additions & 3 deletions fixtures/unit/mixins/one.scss

This file was deleted.

3 changes: 0 additions & 3 deletions fixtures/unit/mixins/two.scss

This file was deleted.

1 change: 1 addition & 0 deletions fixtures/unit/scanner/follow-links/namespace/_index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward "./variables" as var-*;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$var: 1px;
1 change: 1 addition & 0 deletions fixtures/unit/scanner/follow-links/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@use "namespace";
3 changes: 3 additions & 0 deletions fixtures/unit/scanner/self-reference/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@use "./styles";

$var: "hmm";
1 change: 0 additions & 1 deletion fixtures/unit/variables/_one.scss

This file was deleted.

2 changes: 0 additions & 2 deletions fixtures/unit/variables/two.scss

This file was deleted.

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",
"description": "Provides code suggestions, documentation and code navigation for modern SCSS",
"version": "2.6.1",
"version": "2.6.2",
"publisher": "SomewhatStationery",
"license": "MIT",
"engines": {
Expand Down
14 changes: 10 additions & 4 deletions src/server/features/completion/completion-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export interface CompletionContext {
originalExtension: SupportedExtensions;
}

const reReturn = /^.*@return/;
const rePropertyValue = /.*:\s*/;
const reEmptyPropertyValue = /.*:\s*$/;
const reQuotedValueInString = /["'](?:[^"'\\]|\\.)*["']/g;
const reMixinReference = /.*@include\s+(.*)/;
const reComment = /^.*(\/(\/|\*)|\*)/;
const reComment = /^(.*\/\/|.*\/\*|\s*\*)/;
const reSassDoc = /^[\\s]*\/{3}.*$/;
const reQuotes = /["']/;
const rePartialModuleAtRule = /@(?:use|forward|import) ["']/;
Expand All @@ -35,9 +36,10 @@ function checkVariableContext(
isPropertyValue: boolean,
isEmptyValue: boolean,
isQuotes: boolean,
isReturn: boolean,
isNamespace: boolean,
): boolean {
if (isPropertyValue && !isEmptyValue && !isQuotes) {
if ((isReturn || isPropertyValue) && !isEmptyValue && !isQuotes) {
if (isNamespace && word.endsWith(".")) {
return true;
}
Expand Down Expand Up @@ -71,10 +73,11 @@ function checkFunctionContext(
isPropertyValue: boolean,
isEmptyValue: boolean,
isQuotes: boolean,
isReturn: boolean,
isNamespace: boolean,
settings: ISettings,
): boolean {
if (isPropertyValue && !isEmptyValue && !isQuotes) {
if ((isReturn || isPropertyValue) && !isEmptyValue && !isQuotes) {
if (isNamespace) {
return true;
}
Expand Down Expand Up @@ -114,7 +117,7 @@ function checkNamespaceContext(

// Skip #{ if this is interpolation
return currentWord.substring(
isInterpolation ? 2 : 0,
isInterpolation ? currentWord.indexOf("{") + 1 : 0,
currentWord.indexOf("."),
);
}
Expand All @@ -134,6 +137,7 @@ export function createCompletionContext(
const isInterpolation = isInterpolationContext(currentWord);

// Information about current position
const isReturn = reReturn.test(textBeforeWord);
const isPropertyValue = rePropertyValue.test(textBeforeWord);
const isEmptyValue = reEmptyPropertyValue.test(textBeforeWord);
const isQuotes = reQuotes.test(
Expand Down Expand Up @@ -161,6 +165,7 @@ export function createCompletionContext(
isPropertyValue,
isEmptyValue,
isQuotes,
isReturn,
Boolean(namespace),
),
function: checkFunctionContext(
Expand All @@ -169,6 +174,7 @@ export function createCompletionContext(
isPropertyValue,
isEmptyValue,
isQuotes,
isReturn,
Boolean(namespace),
settings,
),
Expand Down
3 changes: 2 additions & 1 deletion src/server/features/completion/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ function traverseTree(
if (
!child.link.target ||
(child as ScssImport).dynamic ||
(child as ScssImport).css
(child as ScssImport).css ||
child.link.target === scssDocument.uri
) {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/features/diagnostics/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ function traverseTree(
if (
!child.link.target ||
(child as ScssImport).dynamic ||
(child as ScssImport).css
(child as ScssImport).css ||
child.link.target === scssDocument.uri
) {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/features/go-definition/go-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ function traverseTree(
if (
!child.link.target ||
(child as ScssImport).dynamic ||
(child as ScssImport).css
(child as ScssImport).css ||
child.link.target === scssDocument.uri
) {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/features/hover/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ function traverseTree(
if (
!child.link.target ||
(child as ScssImport).dynamic ||
(child as ScssImport).css
(child as ScssImport).css ||
child.link.target === scssDocument.uri
) {
continue;
}
Expand Down
10 changes: 8 additions & 2 deletions src/server/features/signature-help/signature-help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ export async function doSignatureHelp(
);

const sassdoc = applySassDoc(symbol, {
displayOptions: { description: true, deprecated: true, return: true },
displayOptions: {
description: true,
deprecated: true,
return: true,
parameter: true,
},
});

signatureInfo.documentation = {
Expand Down Expand Up @@ -347,7 +352,8 @@ function traverseTree(
if (
!child.link.target ||
(child as ScssImport).dynamic ||
(child as ScssImport).css
(child as ScssImport).css ||
child.link.target === scssDocument.uri
) {
continue;
}
Expand Down
37 changes: 34 additions & 3 deletions src/server/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,27 @@ async function findDocumentSymbols(
const partialExists = await fs.exists(partialUri);
if (!partialExists) {
// We tried to resolve the file as a partial, but it doesn't exist.
continue;
// The target string may be a folder with an index file
// so try looking for it by that name.
const index = ensureIndex(link.target);
const indexUri = URI.parse(index);
const indexExists = await fs.exists(indexUri);
if (!indexExists) {
const partialIndex = ensurePartial(ensureIndex(link.target));
const partialIndexUri = URI.parse(partialIndex);
const partialIndexExists = await fs.exists(partialIndexUri);
if (!partialIndexExists) {
// We tried, this file doesn't exist
continue;
} else {
link.target = partialIndex;
}
} else {
link.target = index;
}
} else {
link.target = partial;
}

link.target = partial;
}

const matchUse = reUse.exec(line);
Expand Down Expand Up @@ -270,6 +287,20 @@ function ensurePartial(target: string): string {
return `${path}_${fileName}${extension}`;
}

function ensureIndex(target: string): string {
const lastSlash = target.lastIndexOf("/");
const lastDot = target.lastIndexOf(".");
const fileName = target.substring(lastSlash + 1, lastDot);

if (fileName.includes("index")) {
return target;
}

const path = target.slice(0, Math.max(0, lastSlash + 1));
const extension = target.slice(Math.max(0, lastDot));
return `${path}/${fileName}/index${extension}`;
}

function urlMatches(url: string, linkTarget: string): boolean {
let safeUrl = url;
while (/^[./@~]/.exec(safeUrl)) {
Expand Down
36 changes: 36 additions & 0 deletions src/test/e2e/suite/completion/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,42 @@ describe("SCSS Completion Test", function () {
await testCompletion(svelteDocUri, position(28, 40), expectedCompletions);
await testCompletion(astroDocUri, position(31, 40), expectedCompletions);
});

it("Offers namespace completion inside string interpolation with preceeding non-space character", async () => {
const expectedCompletions = [
{
label: "$var-var-variable",
detail: "Variable declared in _variables.scss",
insertText: '".$var-var-variable"',
filterText: '"ns.$var-var-variable"',
},
{
label: "fun-fun-function",
detail: "Function declared in _functions.scss",
insertText: '{"_tabstop":1,"value":".fun-fun-function()"}',
},
];

await testCompletion(docUri, position(26, 20), expectedCompletions);
});

it("Offers namespace completion as part of return statement", async () => {
const expectedCompletions = [
{
label: "$var-var-variable",
detail: "Variable declared in _variables.scss",
insertText: '".$var-var-variable"',
filterText: '"ns.$var-var-variable"',
},
{
label: "fun-fun-function",
detail: "Function declared in _functions.scss",
insertText: '{"_tabstop":1,"value":".fun-fun-function()"}',
},
];

await testCompletion(docUri, position(30, 23), expectedCompletions);
});
});

describe("SassDoc Completion Test", () => {
Expand Down
19 changes: 18 additions & 1 deletion src/test/parser/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe("Services/Parser", () => {
uri: "middle/middle.scss",
});
await helpers.makeDocument(storage, ["$tr: 2px;"], fs, {
uri: "moddle/lower/lower.scss",
uri: "middle/lower/lower.scss",
});

const document = await helpers.makeDocument(
Expand All @@ -151,6 +151,23 @@ describe("Services/Parser", () => {

strictEqual(uses.length, 3, "expected to find three uses");
});

it("should not crash on link to the same document", async () => {
const document = await helpers.makeDocument(
storage,
['@use "./self";', "$var: 1px;"],
fs,
{
uri: "self.scss",
},
);
const symbols = await parseDocument(document, URI.parse(""), fs);
const uses = [...symbols.uses.values()];
const variables = [...symbols.variables.values()];

strictEqual(variables.length, 1, "expected to find one variable");
strictEqual(uses.length, 0, "expected to find no use link to self");
});
});

describe("regular expressions", () => {
Expand Down
10 changes: 10 additions & 0 deletions src/test/scanner/scanner-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as path from "path";
import { URI } from "vscode-uri";

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

export function getUri(p: string) {
return URI.file(getDocPath(p));
}
Loading

0 comments on commit 055664a

Please sign in to comment.