Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle same tag name. Fix #2049 #2056

Merged
merged 2 commits into from
Jul 31, 2020
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Remove leading empty line in diagnostic errors. #2067.
- `"vetur.completion.tagCasing": "initial"` causes double tag completion. #2053
- 🙌 Fix initializationOptions: Cannot read property 'config' of undefined. Thanks to contribution from [Dawid Pakuła](https://github.com/zulus). #1897 and #1341.
- 🙌 Component props auto-completion doesn't work when using PascalCase. Thanks to contribution from [@yoyo930021](@yoyo930021). #1841 and #2056.
- 🙌 When passing incorrect first arg to vti, show help message. Thanks to contribution from [Rafal Tynski](@rafalt). #1841.
- 🙌 Use CodeAction over command. Thanks to contribution from [Matt Bierner](@mjbvz). #1704.

Expand All @@ -18,7 +19,7 @@
- 🙌 Cusom tags IntelliSense for local `tags.json`/`attributes.json`. [Usage Docs](https://vuejs.github.io/vetur/framework.html#workspace-custom-tags). Thanks to contribution from [Carlos Rodrigues](https://github.com/pikax). #1364 and #2018.
- 🙌 Detect tags from @nuxt/components. Thanks to contribution from [pooya parsa](https://github.com/pi0). #1921.
- 🙌 Fix VTI crash by passing correct PID to language server. Thanks to contribution from [Daniil Yastremskiy](@TheBeastOfCaerbannog). #1699 and #1805.
- 🙌 Fix template interpolation hover info of v-for readonly array item. Thanks to contribution from [@yoyo930021](https://github.com/yoyo930021). #1788.
- 🙌 Fix template interpolation hover info of v-for readonly array item. Thanks to contribution from [@yoyo930021](@yoyo930021). #1788.
- 🙌 Improve performance while using template interpolation service. Thanks to contribution from [@IWANABETHATGUY](https://github.com/IWANABETHATGUY). #1839.

### 0.24.0 | 2020-03-04 | [VSIX](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/octref/vsextensions/vetur/0.24.0/vspackage)
Expand Down
6 changes: 2 additions & 4 deletions server/src/modes/template/services/htmlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ export function doComplete(
const value = isFollowedBy(text, nameEnd, ScannerState.AfterAttributeName, TokenType.DelimiterAssign)
? ''
: '="$1"';
const tag = currentTag.toLowerCase();
tagProviders.forEach(provider => {
const priority = provider.priority;
provider.collectAttributes(tag, (attribute, type, documentation) => {
provider.collectAttributes(currentTag, (attribute, type, documentation) => {
if ((type === 'event' && filterPrefix !== '@') || (type !== 'event' && filterPrefix === '@')) {
return;
}
Expand Down Expand Up @@ -229,10 +228,9 @@ export function doComplete(
range = getReplaceRange(valueStart, valueEnd);
addQuotes = true;
}
const tag = currentTag.toLowerCase();
const attribute = currentAttributeName.toLowerCase();
tagProviders.forEach(provider => {
provider.collectValues(tag, attribute, value => {
provider.collectValues(currentTag, attribute, value => {
const insertText = addQuotes ? '"' + value + '"' : value;
result.items.push({
label: value,
Expand Down
1 change: 0 additions & 1 deletion server/src/modes/template/services/htmlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export function doHover(
}

function getAttributeHover(tag: string, attribute: string, range: Range): Hover {
tag = tag.toLowerCase();
let hover: Hover = NULL_HOVER;
for (const provider of tagProviders) {
provider.collectAttributes(tag, (attr, type, documentation) => {
Expand Down
16 changes: 7 additions & 9 deletions server/src/modes/template/tagProviders/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MarkupContent } from 'vscode-languageserver-types';
import { kebabCase } from 'lodash';

interface TagCollector {
(tag: string, documentation: string | MarkupContent): void;
Expand Down Expand Up @@ -50,6 +51,10 @@ export interface IValueSets {
[tag: string]: string[];
}

export function getSameTagInSet<T>(tagSet: Record<string, T>, tag: string): T | undefined {
return tagSet[tag] ?? tagSet[tag.toLowerCase()] ?? tagSet[kebabCase(tag)];
}

export function collectTagsDefault(collector: TagCollector, tagSet: ITagSet): void {
for (const tag in tagSet) {
collector(tag, tagSet[tag].label);
Expand All @@ -63,14 +68,7 @@ export function collectAttributesDefault(
globalAttributes: StandaloneAttribute[]
): void {
if (tag) {
let tags = tagSet[tag];
if (!tags) {
for (const t in tagSet) {
if (t.toLowerCase() === tag) {
tags = tagSet[t];
}
}
}
const tags = getSameTagInSet(tagSet, tag);

if (tags) {
const attributes = tags.attributes;
Expand Down Expand Up @@ -110,7 +108,7 @@ export function collectValuesDefault(
}
}
if (tag) {
const tags = tagSet[tag];
const tags = getSameTagInSet(tagSet, tag);
if (tags) {
const attributes = tags.attributes;
if (attributes) {
Expand Down
20 changes: 10 additions & 10 deletions server/src/modes/template/tagProviders/externalTagProviders.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as ts from 'typescript';
import * as fs from 'fs';
import * as path from 'path';
import { kebabCase } from 'lodash';

import { IHTMLTagProvider, Priority } from './common';
import { IHTMLTagProvider, Priority, getSameTagInSet } from './common';

import * as elementTags from 'element-helper-json/element-tags.json';
import * as elementAttributes from 'element-helper-json/element-attributes.json';
Expand Down Expand Up @@ -77,7 +78,12 @@ export function getDependencyTagProvider(workspacePath: string, depPkgJson: any)

export function getExternalTagProvider(id: string, tags: any, attributes: any): IHTMLTagProvider {
function findAttributeDetail(tag: string, attr: string) {
return attributes[attr] || attributes[tag + '/' + attr];
return (
attributes[attr] ||
attributes[`${tag}/${attr}`] ||
attributes[`${tag.toLowerCase}/${attr}`] ||
attributes[`${kebabCase(tag)}/${attr}`]
);
}

return {
Expand All @@ -89,10 +95,7 @@ export function getExternalTagProvider(id: string, tags: any, attributes: any):
}
},
collectAttributes(tag, collector) {
if (!tags[tag]) {
return;
}
const attrs = tags[tag].attributes;
const attrs = getSameTagInSet<any>(tags, tag)?.attributes;
if (!attrs) {
return;
}
Expand All @@ -102,10 +105,7 @@ export function getExternalTagProvider(id: string, tags: any, attributes: any):
}
},
collectValues(tag, attr, collector) {
if (!tags[tag]) {
return;
}
const attrs = tags[tag].attributes;
const attrs = getSameTagInSet<any>(tags, tag)?.attributes;
if (!attrs || attrs.indexOf(attr) < 0) {
return;
}
Expand Down
22 changes: 21 additions & 1 deletion test/interpolation/features/completion/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Should autocomplete interpolation for <template>', () => {
});

it(`completes child component tag`, async () => {
await testCompletion(parentTemplateDocUri, position(4, 5), [
await testCompletion(parentTemplateDocUri, position(6, 5), [
{
label: 'basic',
kind: CompletionItemKind.Property,
Expand All @@ -92,6 +92,26 @@ describe('Should autocomplete interpolation for <template>', () => {
]);
});

it(`completes child component's props when kebab case component name`, async () => {
await testCompletion(parentTemplateDocUri, position(4, 15), [
{
label: 'bar',
kind: CompletionItemKind.Value,
documentation: new MarkdownString('My bar').appendCodeblock(`bar: String`, 'js')
}
]);
});

it(`completes child component's props when camel case component name`, async () => {
await testCompletion(parentTemplateDocUri, position(5, 14), [
{
label: 'bar',
kind: CompletionItemKind.Value,
documentation: new MarkdownString('My bar').appendCodeblock(`bar: String`, 'js')
}
]);
});

it('completes inside v-if=""', async () => {
await testCompletion(parentTemplateDocUri, position(3, 17), defaultList);
});
Expand Down
6 changes: 5 additions & 1 deletion test/interpolation/fixture/completion/Parent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
<div>
<basic :></basic>
<basic v-if="" @click="" :foo=""></basic>
<test-comp />
<TestComp />
<
</div>
</template>

<script>
import Basic from './Basic.vue'
import TestComp from './TestComp.vue'

export default {
components: {
Basic
Basic,
TestComp
},
props: {
/**
Expand Down
20 changes: 20 additions & 0 deletions test/interpolation/fixture/completion/TestComp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<template>
<div>{{ bar }}</div>
</template>

<script>
export default {
name: 'TestComp',
props: {
/**
* My bar
*/
bar: String
},
data () {
return {
count: 0
}
}
}
</script>