-
-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Document Formatting (#1732)
* Implement Document Formatting * Update changelog * Remove unused parameters
- Loading branch information
1 parent
190ba22
commit a839120
Showing
11 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
language-service/javascript/src/getGherkinFormattingEdits.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { TextEdit } from "vscode-languageserver-types"; | ||
import { parseGherkinDocument } from './parseGherkinDocument' | ||
import { pretty } from '@cucumber/gherkin-utils' | ||
|
||
// https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#textDocument_formatting | ||
export function getGherkinFormattingEdits(gherkinSource: string): TextEdit[] { | ||
const { gherkinDocument } = parseGherkinDocument(gherkinSource) | ||
const newText = pretty(gherkinDocument) | ||
const lines = gherkinSource.split(/\r?\n/) | ||
const line = lines.length - 1 | ||
const character = lines[line].length | ||
const textEdit: TextEdit = { | ||
newText, | ||
range: { | ||
start: { | ||
line: 0, | ||
character: 0 | ||
}, | ||
end: { | ||
line, | ||
character | ||
} | ||
} | ||
} | ||
return [textEdit] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './getGherkinSemanticTokens' | ||
export * from './getGherkinDiagnostics' | ||
export * from './getGherkinCompletionItems' | ||
export * from './getGherkinFormattingEdits' |
30 changes: 30 additions & 0 deletions
30
language-service/javascript/test/getGherkinFormattingEdits.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import assert from 'assert' | ||
import { TextEdit } from 'vscode-languageserver-types' | ||
import { getGherkinFormattingEdits } from '../src' | ||
|
||
describe('getGherkinFormattingEdits', () => { | ||
it('returns text edits that prettifies a Gherkin document', () => { | ||
const gherkinSource = `Feature: Hello | ||
Scenario: World | ||
Given something` | ||
const textEdits = getGherkinFormattingEdits(gherkinSource) | ||
const expectedTextEdit: TextEdit = { | ||
newText: `Feature: Hello | ||
Scenario: World | ||
Given something | ||
`, | ||
range: { | ||
start: { | ||
line: 0, | ||
character: 0, | ||
}, | ||
end: { | ||
line: 2, | ||
character: 15 | ||
} | ||
} | ||
} | ||
assert.deepStrictEqual([expectedTextEdit], textEdits) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters