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

Implement Document Formatting #1732

Merged
merged 4 commits into from
Sep 7, 2021
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
4 changes: 4 additions & 0 deletions language-server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

* Document Formatting
([#1732](https://github.com/cucumber/common/pull/1732)
[aslakhellesoy])

### Changed

### Deprecated
Expand Down
8 changes: 8 additions & 0 deletions language-server/javascript/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TextDocumentPositionParams,
TextDocuments,
TextDocumentSyncKind,
TextEdit,
} from 'vscode-languageserver/node'

import { TextDocument } from 'vscode-languageserver-textdocument'
Expand All @@ -17,6 +18,7 @@ import {
getGherkinCompletionItems,
getGherkinDiagnostics,
getGherkinSemanticTokens,
getGherkinFormattingEdits,
semanticTokenModifiers,
semanticTokenTypes,
} from '@cucumber/language-service'
Expand Down Expand Up @@ -77,6 +79,7 @@ connection.onInitialize(async (params: InitializeParams) => {
},
}
: undefined,
documentFormattingProvider: true,
},
}
if (hasWorkspaceFolderCapability) {
Expand Down Expand Up @@ -164,6 +167,11 @@ connection.languages.semanticTokens.on((semanticTokenParams: SemanticTokensParam
return getGherkinSemanticTokens(gherkinSource, indexAndExpressions.expressions)
})

connection.onDocumentFormatting(async (params): Promise<TextEdit[]> => {
const gherkinSource = documents.get(params.textDocument.uri).getText()
return getGherkinFormattingEdits(gherkinSource)
})

documents.listen(connection)

connection.listen()
4 changes: 4 additions & 0 deletions language-service/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

* Document Formatting
([#1732](https://github.com/cucumber/common/pull/1732)
[aslakhellesoy])

### Changed

### Deprecated
Expand Down
1 change: 1 addition & 0 deletions language-service/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ and [Cucumber Monaco](../../monaco/javascript).

## Supported features

- [x] Formatting / pretty printing
- [x] Handle parse errors
- [x] Code completion
- [x] Steps
Expand Down
26 changes: 26 additions & 0 deletions language-service/javascript/src/getGherkinFormattingEdits.ts
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]
}
1 change: 1 addition & 0 deletions language-service/javascript/src/index.ts
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 language-service/javascript/test/getGherkinFormattingEdits.test.ts
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
Copy link
Contributor

@aurelien-reeves aurelien-reeves Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using reindent-template-literals (as a dev dependency) to avoid breaking the indentation, as it is done with cucumber-js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gherkinSource is intentionally unindented and the test verifies that it's indented after formatting.

Why do we need to worry about breaking indentation? (Not quite sure what you mean).

Copy link
Contributor

@aurelien-reeves aurelien-reeves Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
})
})
4 changes: 4 additions & 0 deletions monaco/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

* Document Formatting
([#1732](https://github.com/cucumber/common/pull/1732)
[aslakhellesoy])

### Changed

### Deprecated
Expand Down
11 changes: 11 additions & 0 deletions monaco/javascript/example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
<title>Cucumber Monaco</title>
</head>
<body>
<h1>
Monaco Editor with Cucumber Plugin
</h1>
<p>
Things you can try:
</p>
<ul>
<li>⇧⌥F - Format Document</li>
<li>Modify the first step and see it underlined as undefined</li>
<li>Type a new step and see auto-complete suggestions</li>
</ul>
<script src="./main.bundle.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions monaco/javascript/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Feature: Hello
Scenario: Hi
Given I have 58 cukes in my belly
And this is an undefined step
| some | poorly |
| formatted | table |
`,
language: 'gherkin',
theme: 'vs-dark',
Expand Down
18 changes: 18 additions & 0 deletions monaco/javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getGherkinCompletionItems,
getGherkinDiagnostics,
getGherkinSemanticTokens,
getGherkinFormattingEdits,
semanticTokenModifiers,
semanticTokenTypes
} from '@cucumber/language-service'
Expand Down Expand Up @@ -85,4 +86,21 @@ export function configure(monaco: Monaco, index: Index | undefined, expressions:
}
}
})

// Document Formatting
monaco.languages.registerDocumentFormattingEditProvider('gherkin', {
provideDocumentFormattingEdits: function (model) {
const gherkinSource = model.getValue()
const textEdits = getGherkinFormattingEdits(gherkinSource)
return textEdits.map(textEdit => ({
range: {
startLineNumber: textEdit.range.start.line + 1,
startColumn: textEdit.range.start.character + 1,
endLineNumber: textEdit.range.end.line + 1,
endColumn: textEdit.range.end.character + 1,
},
text: textEdit.newText
}))
}
})
}