Skip to content

Commit

Permalink
Fix #255 Enable ignore support of multiline instructions to all forma…
Browse files Browse the repository at this point in the history
…tters

The range formatter and on type formatters now support ignoring
multilien instructions as well.

Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Apr 11, 2021
1 parent 314c8f7 commit 4c3e634
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ All notable changes to this project will be documented in this file.
- optimized on type formatting so that it does not return unnecessary edits ([rcjsuen/dockerfile-language-service#82](https://github.com/rcjsuen/dockerfile-language-service/issues/82))
- textDocument/formatting
- allow the formatter to skip formatting of instructions that span multiple lines ([#255](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/255))
- textDocument/onTypeFormatting
- allow the formatter to skip formatting of instructions that span multiple lines ([#255](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/255))
- textDocument/rangeFormatting
- allow the formatter to skip formatting of instructions that span multiple lines ([#255](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/255))

### Fixed
- do not validate variable substitutions if found in CMD and ENTRYPOINT ([rcjsuen/dockerfile-utils#89](https://github.com/rcjsuen/dockerfile-utils/issues/89))
Expand Down
26 changes: 26 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,20 @@ connection.onDocumentFormatting((documentFormattingParams: DocumentFormattingPar

connection.onDocumentRangeFormatting((rangeFormattingParams: DocumentRangeFormattingParams): PromiseLike<TextEdit[]> => {
return getDocument(rangeFormattingParams.textDocument.uri).then((document) => {
if (configurationSupport) {
return getFormatterConfiguration(document.uri).then((configuration: FormatterConfiguration) => {
if (document) {
const options: FormatterSettings = rangeFormattingParams.options;
options.ignoreMultilineInstructions = configuration !== null && configuration.ignoreMultilineInstructions;
return service.formatRange(document.getText(),rangeFormattingParams.range, options);
}
return [];
});
}

if (document) {
const options: FormatterSettings = rangeFormattingParams.options;
options.ignoreMultilineInstructions = formatterConfiguration !== null && formatterConfiguration.ignoreMultilineInstructions;
return service.formatRange(document.getText(), rangeFormattingParams.range, rangeFormattingParams.options);
}
return [];
Expand All @@ -651,7 +664,20 @@ connection.onDocumentRangeFormatting((rangeFormattingParams: DocumentRangeFormat

connection.onDocumentOnTypeFormatting((onTypeFormattingParams: DocumentOnTypeFormattingParams): PromiseLike<TextEdit[]> => {
return getDocument(onTypeFormattingParams.textDocument.uri).then((document) => {
if (configurationSupport) {
return getFormatterConfiguration(document.uri).then((configuration: FormatterConfiguration) => {
if (document) {
const options: FormatterSettings = onTypeFormattingParams.options;
options.ignoreMultilineInstructions = configuration !== null && configuration.ignoreMultilineInstructions;
return service.formatOnType(document.getText(), onTypeFormattingParams.position, onTypeFormattingParams.ch, options);
}
return [];
});
}

if (document) {
const options: FormatterSettings = onTypeFormattingParams.options;
options.ignoreMultilineInstructions = formatterConfiguration !== null && formatterConfiguration.ignoreMultilineInstructions;
return service.formatOnType(document.getText(), onTypeFormattingParams.position, onTypeFormattingParams.ch, onTypeFormattingParams.options);
}
return [];
Expand Down
158 changes: 137 additions & 21 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,7 @@ describe("Dockerfile LSP Tests", function() {
});
});

function test255(fileName: string, configurationSet: boolean, ignoreMultilineAttribute: any, callback: Function): void {
function test255(fileName: string, text: string, request: string, params: any, configurationSet: boolean, ignoreMultilineAttribute: any, callback: Function): void {
if (configurationSet) {
sendNotification("workspace/didChangeConfiguration", {
settings: {
Expand All @@ -1866,19 +1866,12 @@ describe("Dockerfile LSP Tests", function() {
languageId: "dockerfile",
version: 1,
uri: documentURI,
text: "FROM node AS\\\n build"
text: text,
}
});

const requestId = sendRequest("textDocument/formatting", {
textDocument: {
uri: documentURI,
},
options: {
insertSpaces: true,
tabSize: 4
}
});
params.textDocument.uri = documentURI;
const requestId = sendRequest(request, params);

const listener = (json: any) => {
if (json.id === requestId) {
Expand All @@ -1903,19 +1896,142 @@ describe("Dockerfile LSP Tests", function() {
}

describe("issue #255 workspace configuration", () => {
it("workspace configuration not defined", finished => {
this.timeout(5000);
test255("255-workspace-configuration-not-defined", false, null, finished);
});
const fileFormattingRequest = {
textDocument: {},
options: { insertSpaces: true, tabSize: 4 }
};

it("workspace configuration true", finished => {
this.timeout(5000);
test255("255-workspace-configuration-true", true, true, finished);
});
const rangeFormattingRequest = {
textDocument: {},
range: {
start: { line: 0, position: 3 },
end: { line: 1, position: 3 }
},
options: { insertSpaces: true, tabSize: 4 }
};

it("workspace configuration false", finished => {
const onTypeFormattingRequest = {
textDocument: {},
position: { line: 0, position: 12 },
ch: "\\",
options: { insertSpaces: true, tabSize: 4 }
};

it("file formatting workspace configuration not defined", finished => {
this.timeout(5000);
test255(
"file-255-workspace-configuration-not-defined",
"FROM node AS\\\n build",
"textDocument/formatting",
fileFormattingRequest,
false,
null,
finished
);
});

it("file formatting workspace configuration true", finished => {
this.timeout(5000);
test255(
"file-255-workspace-configuration-not-defined",
"FROM node AS\\\n build",
"textDocument/formatting",
fileFormattingRequest,
true,
true,
finished
);
});

it("file formatting workspace configuration false", finished => {
this.timeout(5000);
test255(
"file-255-workspace-configuration-not-defined",
"FROM node AS\\\n build",
"textDocument/formatting",
fileFormattingRequest,
true,
false,
finished
);
});

it("range formatting workspace configuration not defined", finished => {
this.timeout(5000);
test255(
"range-255-workspace-configuration-not-defined",
"FROM node AS\\\n build\nFROM node AS \\\n build",
"textDocument/rangeFormatting",
rangeFormattingRequest,
false,
null,
finished
);
});

it("range formatting workspace configuration true", finished => {
this.timeout(5000);
test255(
"range-255-workspace-configuration-not-defined",
"FROM node AS\\\n build\nFROM node AS \\\n build",
"textDocument/rangeFormatting",
rangeFormattingRequest,
true,
true,
finished
);
});

it("range formatting workspace configuration false", finished => {
this.timeout(5000);
test255(
"range-255-workspace-configuration-not-defined",
"FROM node AS\\\n build\nFROM node AS \\\n build",
"textDocument/rangeFormatting",
rangeFormattingRequest,
true,
false,
finished
);
});

it("on type formatting workspace configuration not defined", finished => {
this.timeout(5000);
test255(
"on-type-255-workspace-configuration-not-defined",
"FROM node AS\n build",
"textDocument/onTypeFormatting",
onTypeFormattingRequest,
false,
null,
finished
);
});

it("on type formatting workspace configuration true", finished => {
this.timeout(5000);
test255(
"on-type-255-workspace-configuration-not-defined",
"FROM node AS\n build",
"textDocument/onTypeFormatting",
onTypeFormattingRequest,
true,
true,
finished
);
});

it("on type formatting workspace configuration false", finished => {
this.timeout(5000);
test255("255-workspace-configuration-false", true, false, finished);
test255(
"on-type-255-workspace-configuration-not-defined",
"FROM node AS\n build",
"textDocument/onTypeFormatting",
onTypeFormattingRequest,
true,
false,
finished
);
});
});

Expand Down
Loading

0 comments on commit 4c3e634

Please sign in to comment.