Skip to content

Commit 6cda66f

Browse files
committed
chore: moving things around
1 parent 62a4a99 commit 6cda66f

File tree

5 files changed

+50
-36
lines changed

5 files changed

+50
-36
lines changed

src/common/xml/XmlSuggestionProvider.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ export abstract class XmlSuggestionProvider<T> {
2424
attribute?: XMLAttribute
2525
): T[];
2626

27-
public getConfigKey(): string | undefined {
27+
protected getConfigKey(): string | undefined {
2828
return undefined;
2929
}
3030

31-
public getAttributeValueConditions(): CombinedCondition[] {
31+
protected getAttributeValueConditions(): CombinedCondition[] {
3232
return [];
3333
}
3434

35-
public getElementContentMatches(): CombinedCondition[] {
35+
protected getElementContentMatches(): CombinedCondition[] {
3636
return [];
3737
}
3838

@@ -48,22 +48,18 @@ export abstract class XmlSuggestionProvider<T> {
4848
return this.processSuggestions(document, position, tokenData);
4949
}
5050

51-
public getSuggestionProviders(document: TextDocument): SuggestionProviders<T> {
51+
protected getSuggestionProviders(document: TextDocument): SuggestionProviders<T> {
5252
return {
5353
attributeValue: [options => this.getAttributeValueSuggestionProviders(document, options)],
5454
elementContent: [options => this.getElementContentSuggestionProviders(document, options)],
5555
};
5656
}
5757

58-
public getAttributeValueSuggestionProviders(
58+
protected getAttributeValueSuggestionProviders(
5959
document: TextDocument,
6060
{ element, attribute }: AttributeValueCompletionOptions<undefined>
6161
): T[] {
62-
const match = this.getAttributeValueConditions().find(matchElement => {
63-
return this.matchesConditions(matchElement, element, attribute);
64-
});
65-
66-
if (!match) {
62+
if (!this.hasMatchingCondition(this.getAttributeValueConditions(), element, attribute)) {
6763
return [];
6864
}
6965

@@ -81,15 +77,11 @@ export abstract class XmlSuggestionProvider<T> {
8177
return this.getSuggestionItems(value, range, document, element, attribute);
8278
}
8379

84-
public getElementContentSuggestionProviders(
80+
protected getElementContentSuggestionProviders(
8581
document: TextDocument,
8682
{ element }: ElementContentCompletionOptions<undefined>
8783
): T[] {
88-
const match = this.getElementContentMatches().find(matchElement => {
89-
return this.matchesConditions(matchElement, element);
90-
});
91-
92-
if (!match) {
84+
if (!this.hasMatchingCondition(this.getElementContentMatches(), element)) {
9385
return [];
9486
}
9587

@@ -108,6 +100,16 @@ export abstract class XmlSuggestionProvider<T> {
108100
return this.getSuggestionItems(elementValue, range, document, element);
109101
}
110102

103+
protected hasMatchingCondition(
104+
conditions: CombinedCondition[],
105+
element: XMLElement,
106+
attribute?: XMLAttribute
107+
): boolean {
108+
return conditions.some(condition => {
109+
return this.matchesConditions(condition, element, attribute);
110+
});
111+
}
112+
111113
protected matchesConditions(
112114
conditions: CombinedCondition,
113115
element: XMLElement,
@@ -139,8 +141,10 @@ export abstract class XmlSuggestionProvider<T> {
139141
}
140142
}
141143

142-
return this.getFilePatterns().some(pattern =>
143-
minimatch(document.uri.fsPath, pattern, { matchBase: true })
144-
);
144+
return this.isMatchingFile(document, this.getFilePatterns());
145+
}
146+
147+
protected isMatchingFile(document: TextDocument, patterns: string[]): boolean {
148+
return patterns.some(pattern => minimatch(document.uri.fsPath, pattern, { matchBase: true }));
145149
}
146150
}

src/completion/XmlCompletionProviderProcessor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export class XmlCompletionProviderProcessor
1414
extends XmlSuggestionProviderProcessor<CompletionItem>
1515
implements CompletionItemProvider
1616
{
17+
public static readonly TRIGGER_CHARACTERS =
18+
'\\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
19+
1720
public constructor() {
1821
super([
1922
new ModuleCompletionProvider(),

src/completion/XmlSnippetProvider.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class XmlSnippetProvider implements vscode.CompletionItemProvider {
1818
private readonly snippetProviders: SnippetProvider[] = [
1919
{
2020
pattern: '**/di.xml',
21-
snippets: require('./snippets/di-xml.json'),
21+
snippets: require('./xml/snippet/di-xml.json'),
2222
},
2323
];
2424

@@ -28,8 +28,6 @@ export class XmlSnippetProvider implements vscode.CompletionItemProvider {
2828
token: vscode.CancellationToken,
2929
context: vscode.CompletionContext
3030
): vscode.ProviderResult<vscode.CompletionItem[]> {
31-
const wordRange = document.getWordRangeAtPosition(position);
32-
const word = document.getText(wordRange);
3331
const snippets = this.getSnippets(document);
3432

3533
if (!snippets) {
@@ -41,15 +39,17 @@ export class XmlSnippetProvider implements vscode.CompletionItemProvider {
4139
for (const name in snippets) {
4240
const snippet = snippets[name];
4341

44-
// if (snippet.prefix.indexOf(word) !== 0) {
45-
// continue;
46-
// }
47-
48-
const completionItem = new vscode.CompletionItem(name, vscode.CompletionItemKind.Snippet);
49-
completionItem.insertText = new vscode.SnippetString(snippet.body.join('\n'));
42+
const completionItem = new vscode.CompletionItem(
43+
{
44+
label: name,
45+
detail: ' magento-toolbox',
46+
},
47+
vscode.CompletionItemKind.Snippet
48+
);
5049

50+
const snippetString = new vscode.SnippetString(snippet.body.join('\n'));
51+
completionItem.insertText = snippetString;
5152
completionItem.documentation = new vscode.MarkdownString(snippet.description);
52-
5353
completionItem.additionalTextEdits = [
5454
vscode.TextEdit.delete(
5555
new vscode.Range(position.line, position.character - 1, position.line, position.character)
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
{
2-
"Insert a preference element": {
2+
"preference": {
33
"prefix": "preference",
44
"body": [
55
"<preference for=\"${1:ClassName}\" type=\"${2:ClassName}\" />"
66
],
77
"description": "Insert a preference element to the current file"
88
},
9-
"Insert a type element": {
9+
"type": {
1010
"prefix": "type",
1111
"body": [
12-
"<type name=\"${1:ClassName}\">\n</type>"
12+
"<type name=\"${1:ClassName}\">\n ${2}\n</type>"
1313
],
1414
"description": "Insert a type element to the current file"
1515
},
16-
"Insert a arguments element": {
16+
"arguments": {
1717
"prefix": "arguments",
1818
"body": [
19-
"<arguments>\n</arguments>"
19+
"<arguments>\n ${1}\n</arguments>"
2020
],
2121
"description": "Insert an arguments element to the current file"
2222
},
23-
"Insert a argument element": {
23+
"argument": {
2424
"prefix": "argument",
2525
"body": [
2626
"<argument name=\"${1:Name}\" xsi:type=\"${2:Type}\">${3:Value}</argument>"
2727
],
2828
"description": "Insert an argument element to the current file"
29+
},
30+
"virtualType": {
31+
"prefix": "virtualType",
32+
"body": [
33+
"<virtualType name=\"${1:ClassName}\" type=\"${2:ClassName}\">\n ${3}\n</virtualType>"
34+
],
35+
"description": "Insert a virtualType element to the current file"
2936
}
3037
}

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export async function activate(context: vscode.ExtensionContext) {
108108
vscode.languages.registerCompletionItemProvider(
109109
'xml',
110110
new XmlCompletionProviderProcessor(),
111-
...'\\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
111+
...XmlCompletionProviderProcessor.TRIGGER_CHARACTERS
112112
),
113113
vscode.languages.registerCompletionItemProvider(
114114
'xml',

0 commit comments

Comments
 (0)