Skip to content

Commit 5be3d9d

Browse files
authored
Only send yaml files to the formatter (#231)
1 parent 5fe17c2 commit 5be3d9d

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

server/src/cfnServerInit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { SettingsState } from './cfnSettings';
2020
import { ValidationHandler } from './handlers/validationHandler';
2121
import { SettingsHandler } from './handlers/settingsHandler';
2222
import { NotificationHandler } from './handlers/notificationHandler';
23-
import { LanguageHandlers } from 'yaml-language-server/out/server/src/languageserver/handlers/languageHandlers';
23+
import { LanguageHandlers } from './handlers/languageHandlers';
2424
import { Telemetry } from 'yaml-language-server/out/server/src/languageserver/telemetry';
2525
import { readFile } from 'fs';
2626
import { JSONSchema } from 'yaml-language-server/out/server/src/languageservice/jsonSchema';

server/src/handlers/helpers.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Connection } from 'vscode-languageserver';
2+
3+
export function isCloudFormation(template: string, filename: string, cfnConnection: Connection): Boolean {
4+
5+
if (/"?AWSTemplateFormatVersion"?\s*/.exec(template)) {
6+
cfnConnection.console.log("Determined this file is a CloudFormation Template. " + filename +
7+
". Found the string AWSTemplateFormatVersion");
8+
return true;
9+
}
10+
if (/\n?"?Resources"?\s*:/.exec(template)) {
11+
if (/"?Type"?\s*:\s*"?'?[a-zA-Z0-9]{2,64}::[a-zA-Z0-9]{2,64}/.exec(template)) {
12+
// filter out serverless.io templates
13+
if (!(/\nresources:/.exec(template) && /\nprovider:/.exec(template))) {
14+
cfnConnection.console.log("Determined this file is a CloudFormation Template. " + filename +
15+
". Found 'Resources' and 'Type: [a-zA-Z0-9]{2,64}::[a-zA-Z0-9]{2,64}'");
16+
return true;
17+
}
18+
}
19+
}
20+
return false;
21+
}
22+
23+
export function isYaml(filename: string): Boolean {
24+
25+
if (filename.endsWith('.yaml') || filename.endsWith('.yml')) {
26+
return true;
27+
}
28+
return false;
29+
}

server/src/handlers/languageHandlers.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ permissions and limitations under the License.
1616
import { Connection } from 'vscode-languageserver';
1717
import {
1818
TextDocumentPositionParams,
19+
DocumentFormattingParams,
1920
} from 'vscode-languageserver-protocol';
2021
import {
2122
CompletionList,
23+
TextEdit,
2224
} from 'vscode-languageserver-types';
2325
import { LanguageService } from 'yaml-language-server';
2426
import { SettingsState } from '../cfnSettings';
2527
import { ValidationHandler } from './validationHandler';
2628
import { LanguageHandlers as YamlLanguageHandlers } from 'yaml-language-server/out/server/src/languageserver/handlers/languageHandlers';
29+
import { isYaml } from './helpers';
2730

2831
// code adopted from https://github.com/redhat-developer/yaml-language-server/blob/main/src/languageserver/handlers/languageHandlers.ts
2932
export class LanguageHandlers extends YamlLanguageHandlers{
@@ -65,4 +68,33 @@ export class LanguageHandlers extends YamlLanguageHandlers{
6568
false,
6669
);
6770
}
71+
72+
/**
73+
* Called when the formatter is invoked
74+
* Returns the formatted document content using prettier
75+
*/
76+
formatterHandler(formatParams: DocumentFormattingParams): TextEdit[] {
77+
const uri = formatParams.textDocument.uri;
78+
const document = this.cfnSettings.documents.get(uri);
79+
80+
if (!document) {
81+
// @ts-ignore
82+
return;
83+
}
84+
85+
let fileIsYaml = isYaml(uri.toString());
86+
87+
if (!fileIsYaml) {
88+
// @ts-ignore
89+
return;
90+
}
91+
92+
const customFormatterSettings = {
93+
tabWidth: formatParams.options.tabSize,
94+
...this.cfnSettings.yamlFormatterSettings,
95+
};
96+
97+
return this.cfnLanguageService.doFormat(document, customFormatterSettings);
98+
}
99+
68100
}

server/src/handlers/validationHandler.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { CfnLint } from '../services/cfnlint';
2323
import { Diagnostic } from 'vscode-languageserver-types';
2424
import { LanguageService } from 'yaml-language-server';
2525
import { ValidationHandler as YamlValidationHandler } from 'yaml-language-server/out/server/src/languageserver/handlers/validationHandlers';
26-
26+
import { isCloudFormation } from './helpers';
2727

2828
// code adopted from https://github.com/redhat-developer/yaml-language-server/blob/main/src/languageserver/handlers/validationHandlers.ts
2929
export class ValidationHandler extends YamlValidationHandler {
@@ -43,26 +43,6 @@ export class ValidationHandler extends YamlValidationHandler {
4343
});
4444
}
4545

46-
private isCloudFormation(template: string, filename: string): Boolean {
47-
48-
if (/"?AWSTemplateFormatVersion"?\s*/.exec(template)) {
49-
this.cfnConnection.console.log("Determined this file is a CloudFormation Template. " + filename +
50-
". Found the string AWSTemplateFormatVersion");
51-
return true;
52-
}
53-
if (/\n?"?Resources"?\s*:/.exec(template)) {
54-
if (/"?Type"?\s*:\s*"?'?[a-zA-Z0-9]{2,64}::[a-zA-Z0-9]{2,64}/.exec(template)) {
55-
// filter out serverless.io templates
56-
if (!(/\nresources:/.exec(template) && /\nprovider:/.exec(template))) {
57-
this.cfnConnection.console.log("Determined this file is a CloudFormation Template. " + filename +
58-
". Found 'Resources' and 'Type: [a-zA-Z0-9]{2,64}::[a-zA-Z0-9]{2,64}'");
59-
return true;
60-
}
61-
}
62-
}
63-
return false;
64-
}
65-
6646
private patchTemplateSchema(registrySchemaDirectory: string) {
6747
const stub = readFileSync(__dirname + '/../../schema/resource-patch-stub.json', 'utf8');
6848
let templateSchema = JSON.parse(readFileSync(__dirname + '/../../schema/all-spec.json', 'utf8'));
@@ -85,7 +65,7 @@ export class ValidationHandler extends YamlValidationHandler {
8565

8666
let fileToLint = URI.parse(uri).fsPath;
8767

88-
let isCfn = this.isCloudFormation(document.getText(), uri.toString());
68+
let isCfn = isCloudFormation(document.getText(), uri.toString(), this.cfnConnection);
8969

9070
this.cfnConnection.sendNotification('cfn/isPreviewable', isCfn);
9171

0 commit comments

Comments
 (0)