Skip to content

Commit

Permalink
fix(lsp): initial inferred selector according to flag
Browse files Browse the repository at this point in the history
- expanded tests to match both cases
  • Loading branch information
idoros committed Oct 3, 2023
1 parent 7bb14c9 commit f2601e9
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 56 deletions.
11 changes: 10 additions & 1 deletion packages/language-service/src/lib-new/lang-service-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
StylableProcessor,
StylableTransformer,
InferredSelector,
CSSType,
} from '@stylable/core/dist/index-internal';
import { URI } from 'vscode-uri';
import {
Expand Down Expand Up @@ -101,7 +102,15 @@ export class LangServiceContext {
}
const inferredSelectorContext = new InferredSelector(
transformer,
this.stylable.resolver.resolveExtends(this.meta, 'root')
transformer.experimentalSelectorInference
? new InferredSelector(transformer, [
{
_kind: 'css',
meta: this.meta,
symbol: CSSType.createSymbol({ name: '*' }),
},
])
: this.stylable.resolver.resolveExtends(this.meta, 'root')
);
// reference interest points
const locationSelector = this.location.selector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,35 @@ describe('LS: css-pseudo-class', () => {
}));
});
it('should suggest root custom states in empty selector', () => {
// ToDo: once experimentalSelectorInference is on this should not behave like this
const { service, assertCompletions } = testLangService(`
const source = `
.root {
-st-states: aaa, bbb;
}
^empty^ {}
`);
`;
const { service, assertCompletions } = testLangService(source);
const entryPath = '/entry.st.css';

assertCompletions(entryPath, ({ filePath, carets }) => ({
message: 'empty',
actualList: service.onCompletion(filePath, carets.empty),
expectedList: [{ label: ':aaa' }, { label: ':bbb' }],
unexpectedList: [{ label: ':aaa' }, { label: ':bbb' }],
}));

{
// with experimentalSelectorInference=false
const { service, assertCompletions } = testLangService(source, {
stylableConfig: { experimentalSelectorInference: false },
});
const entryPath = '/entry.st.css';

assertCompletions(entryPath, ({ filePath, carets }) => ({
message: 'empty',
actualList: service.onCompletion(filePath, carets.empty),
expectedList: [{ label: ':aaa' }, { label: ':bbb' }],
}));
}
});
it('should NOT suggest used states', () => {
const { service, assertCompletions } = testLangService(`
Expand Down Expand Up @@ -285,7 +299,7 @@ describe('LS: css-pseudo-class', () => {
});
describe('st-scope', () => {
it('should suggest class custom states (in st-scope params)', () => {
const { service, assertCompletions } = testLangService(`
const source = `
.root {
-st-states: aaa,bbb;
}
Expand All @@ -294,7 +308,8 @@ describe('LS: css-pseudo-class', () => {
@st-scope .root^afterRoot^ {}
@st-scope ^empty^ {}
`);
`;
const { service, assertCompletions } = testLangService(source);

assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
actualList: service.onCompletion(filePath, carets.afterRoot),
Expand All @@ -303,8 +318,25 @@ describe('LS: css-pseudo-class', () => {

assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
actualList: service.onCompletion(filePath, carets.empty),
expectedList: [{ label: ':aaa' }, { label: ':bbb' }],
unexpectedList: [{ label: ':aaa' }, { label: ':bbb' }],
}));

{
// with experimentalSelectorInference=false
const { service, assertCompletions } = testLangService(source, {
stylableConfig: { experimentalSelectorInference: false },
});

assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
actualList: service.onCompletion(filePath, carets.afterRoot),
expectedList: [{ label: ':aaa' }, { label: ':bbb' }],
}));

assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
actualList: service.onCompletion(filePath, carets.empty),
expectedList: [{ label: ':aaa' }, { label: ':bbb' }],
}));
}
});
it('should suggest class custom states with nesting selector', () => {
const { service, assertCompletions } = testLangService(`
Expand Down Expand Up @@ -378,8 +410,8 @@ describe('LS: css-pseudo-class', () => {
})
);
});
it('should suggest root custom states an empty nested selector', () => {
const { service, assertCompletions } = testLangService(`
it('should NOT suggest root custom states after empty nested selector', () => {
const source = `
.root {
-st-states: root-state;
}
Expand All @@ -399,66 +431,97 @@ describe('LS: css-pseudo-class', () => {
:^colonInMedia^
}
}
`);
`;
const { service, assertCompletions } = testLangService(source);

assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
message: 'empty',
actualList: service.onCompletion(filePath, carets.empty),
expectedList: [{ label: ':root-state' }],
unexpectedList: [{ label: ':aaa' }, { label: ':bbb' }],
unexpectedList: [{ label: ':root-state' }, { label: ':aaa' }, { label: ':bbb' }],
}));

assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
message: 'empty in media',
actualList: service.onCompletion(filePath, carets.emptyInMedia),
expectedList: [{ label: ':root-state' }],
unexpectedList: [{ label: ':aaa' }, { label: ':bbb' }],
unexpectedList: [{ label: ':root-state' }, { label: ':aaa' }, { label: ':bbb' }],
}));

assertCompletions(
'/entry.st.css',
({ filePath, carets, textEdit: { replaceText } }) => ({
message: 'colon',
actualList: service.onCompletion(filePath, carets.colon),
expectedList: [
{
label: ':root-state',
textEdit: replaceText(carets.colon, ':root-state', {
deltaStart: -1,
}),
},
],
assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
message: 'colon',
actualList: service.onCompletion(filePath, carets.colon),
unexpectedList: [{ label: ':root-state' }, { label: ':aaa' }, { label: ':bbb' }],
}));

assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
message: 'colonInMedia',
actualList: service.onCompletion(filePath, carets.colonInMedia),
unexpectedList: [{ label: ':root-state' }, { label: ':aaa' }, { label: ':bbb' }],
}));

{
// with experimentalSelectorInference=false
const { service, assertCompletions } = testLangService(source, {
stylableConfig: { experimentalSelectorInference: false },
});

assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
message: 'empty',
actualList: service.onCompletion(filePath, carets.empty),
expectedList: [],
unexpectedList: [{ label: ':aaa' }, { label: ':bbb' }],
})
);
}));

assertCompletions(
'/entry.st.css',
({ filePath, carets, textEdit: { replaceText } }) => ({
message: 'colonInMedia',
actualList: service.onCompletion(filePath, carets.colonInMedia),
expectedList: [
{
label: ':root-state',
textEdit: replaceText(carets.colonInMedia, ':root-state', {
deltaStart: -1,
}),
},
],
assertCompletions('/entry.st.css', ({ filePath, carets }) => ({
message: 'empty in media',
actualList: service.onCompletion(filePath, carets.emptyInMedia),
expectedList: [{ label: ':root-state' }],
unexpectedList: [{ label: ':aaa' }, { label: ':bbb' }],
})
);
}));

assertCompletions(
'/entry.st.css',
({ filePath, carets, textEdit: { replaceText } }) => ({
message: 'colon',
actualList: service.onCompletion(filePath, carets.colon),
expectedList: [
{
label: ':root-state',
textEdit: replaceText(carets.colon, ':root-state', {
deltaStart: -1,
}),
},
],
unexpectedList: [{ label: ':aaa' }, { label: ':bbb' }],
})
);

assertCompletions(
'/entry.st.css',
({ filePath, carets, textEdit: { replaceText } }) => ({
message: 'colonInMedia',
actualList: service.onCompletion(filePath, carets.colonInMedia),
expectedList: [
{
label: ':root-state',
textEdit: replaceText(carets.colonInMedia, ':root-state', {
deltaStart: -1,
}),
},
],
unexpectedList: [{ label: ':aaa' }, { label: ':bbb' }],
})
);
}
});
it('should suggest matching intersection states', () => {
const tempDir = createTempDirectorySync('lps-import-test-');
const { service, assertCompletions, fs } = testLangService(
{
'comp.st.css': `
const sources = {
'comp.st.css': `
.root {
-st-states: comp-state;
}
`,
'entry.st.css': `
'entry.st.css': `
@st-import Comp from './comp.st.css';
.root {
-st-extends: Comp;
Expand All @@ -471,23 +534,27 @@ describe('LS: css-pseudo-class', () => {
-st-states: shared, onlyB;
}
@st-scope .a, .b {
^inScope^
&^nest^
&:^nestColon^
}
`,
},
{ testOnNativeFileSystem: tempDir.path }
);
};
const { service, assertCompletions, fs } = testLangService(sources, {
testOnNativeFileSystem: tempDir.path,
});
const entryPath = fs.join(tempDir.path, 'entry.st.css');

assertCompletions(entryPath, ({ filePath, carets }) => ({
actualList: service.onCompletion(filePath, carets.inScope),
expectedList: [{ label: ':root-state' }, { label: ':comp-state' }],
unexpectedList: [{ label: ':shared' }, { label: ':onlyA' }, { label: ':onlyB' }],
unexpectedList: [
{ label: ':root-state' },
{ label: ':comp-state' },
{ label: ':shared' },
{ label: ':onlyA' },
{ label: ':onlyB' },
],
}));

assertCompletions(entryPath, ({ filePath, carets }) => ({
Expand Down Expand Up @@ -516,6 +583,52 @@ describe('LS: css-pseudo-class', () => {
{ label: ':root-state' },
],
}));

{
// with experimentalSelectorInference=false
const { service, assertCompletions, fs } = testLangService(sources, {
testOnNativeFileSystem: tempDir.path,
stylableConfig: { experimentalSelectorInference: false },
});
const entryPath = fs.join(tempDir.path, 'entry.st.css');

assertCompletions(entryPath, ({ filePath, carets }) => ({
actualList: service.onCompletion(filePath, carets.inScope),
expectedList: [{ label: ':root-state' }, { label: ':comp-state' }],
unexpectedList: [
{ label: ':shared' },
{ label: ':onlyA' },
{ label: ':onlyB' },
],
}));

assertCompletions(entryPath, ({ filePath, carets }) => ({
actualList: service.onCompletion(filePath, carets.nest),
expectedList: [{ label: ':shared' }],
unexpectedList: [
{ label: ':onlyA' },
{ label: ':onlyB' },
{ label: ':root-state' },
],
}));

assertCompletions(entryPath, ({ filePath, carets, textEdit: { replaceText } }) => ({
actualList: service.onCompletion(filePath, carets.nestColon),
expectedList: [
{
label: ':shared',
textEdit: replaceText(carets.nestColon, ':shared', {
deltaStart: -1,
}),
},
],
unexpectedList: [
{ label: ':onlyA' },
{ label: ':onlyB' },
{ label: ':root-state' },
],
}));
}
});
});
describe('nesting', () => {
Expand Down

0 comments on commit f2601e9

Please sign in to comment.