Skip to content

Commit

Permalink
Formatting feature noted in README and CHANGELOG; Reading settings bu…
Browse files Browse the repository at this point in the history
…g fix
  • Loading branch information
igochkov committed Jul 4, 2023
1 parent 668eba5 commit 7f450ce
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Our development roadmap outlines the planned features for EBNF Tools. Features m
- [x] **Go to Definition / Peek Definition**: Quickly navigate to the definition of a symbol in your EBNF code.
- [x] **Code folding (by markers)**: Hide sections of your EBNF code for easier reading and navigation.
- [x] **Find All References**: Find every reference to a specific symbol in your EBNF code.
- [ ] **Formatting**: Automatically format your EBNF code to adhere to a specific style or standard.
- [ ] **Commands (Compile to tmLanguage)**: A planned command for compiling EBNF code to tmLanguage.
- [x] **Formatting**: Automatically format your EBNF code to adhere to a specific style or standard.
- [ ] **IntelliSense - List Members**: Get a list of members for a given symbol.
- [ ] **IntelliSense - Complete Word**: Automatically complete words as you type them.
- [ ] **Commands (Transpile to tmLanguage)**: A planned command for compiling EBNF code to tmLanguage.
- [ ] **Railroad diagram generation**: Generate railroad diagrams from your EBNF code for visualization and documentation purposes.


Expand All @@ -26,9 +26,10 @@ Our development roadmap outlines the planned features for EBNF Tools. Features m
The change log lists the updates for each version that has been released on the official Visual Studio Code extension gallery.

### Version 1.3
**Pending Release**
**Released on 2023-07-04**

- _Find All References_
- _Formatting_ and formatting settings

### Version 1.2
**Released on 2023-06-22**
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ The EBNF Tools extension currently offers the following features:

![Find All References](https://raw.githubusercontent.com/igochkov/vscode-ebnf/main/docs/references.gif)

- **Formatting**: Automatically format your EBNF code to adhere to a specific style or standard.

![Formatting](https://raw.githubusercontent.com/igochkov/vscode-ebnf/main/docs/formatting.gif)

## Contributing

We welcome contributions to the EBNF Tools project. If you want to contribute, please first read our [contribution guidelines](https://github.com/igochkov/vscode-ebnf/blob/main/CONTRIBUTING.md).
Expand Down
Binary file added docs/formatting.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 7 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function activate(context: vscode.ExtensionContext) {
const settings = vscode.workspace.getConfiguration(ParserContext.ebnfConfigurationName);

if (settings.format.enable) {
formattingRegistrations = registerFormatting(settings);
formattingRegistrations = registerFormatting();
}

context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(handleSettingChange));
Expand All @@ -31,17 +31,18 @@ function handleSettingChange(event: vscode.ConfigurationChangeEvent) {
if (event.affectsConfiguration(ParserContext.ebnfConfigurationName)) {
const settings = vscode.workspace.getConfiguration(ParserContext.ebnfConfigurationName);

if (settings.format.enable) {
formattingRegistrations = registerFormatting(settings);
if (settings.format.enable && !formattingRegistrations) {
formattingRegistrations = registerFormatting();
}
else {
else if (!settings.format.enable && formattingRegistrations) {
formattingRegistrations.dispose();
formattingRegistrations = undefined;
}
}
}

function registerFormatting(settings: vscode.WorkspaceConfiguration): vscode.Disposable {
const formattingProvider = new EBNFFormattingProvider(settings);
function registerFormatting(): vscode.Disposable {
const formattingProvider = new EBNFFormattingProvider();

return vscode.Disposable.from(
vscode.languages.registerDocumentFormattingEditProvider(ParserContext.ebnfSelector, formattingProvider),
Expand Down
31 changes: 14 additions & 17 deletions src/providers/FormattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ import { CharStreams, CommonTokenStream } from "antlr4ts";
import { EBNFParser } from "../parser/EBNFParser";
import { FormattingVisitor } from "../visitors/FormattingVisitor";
import { EBNFFormattingOptions } from "./EBNFFormattingOptions";
import { ParserContext } from "../ParserContext";

export class EBNFFormattingProvider implements vscode.DocumentFormattingEditProvider, vscode.DocumentRangeFormattingEditProvider, vscode.OnTypeFormattingEditProvider {
private settings: vscode.WorkspaceConfiguration;

constructor(settings: vscode.WorkspaceConfiguration) {
this.settings = settings;
}

provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): vscode.ProviderResult<vscode.TextEdit[]> {
const content: string = document.getText();
const formattedText = this.format(content, options);
Expand Down Expand Up @@ -59,20 +54,22 @@ export class EBNFFormattingProvider implements vscode.DocumentFormattingEditProv
}

private formattingOptions(vsOptions: vscode.FormattingOptions): EBNFFormattingOptions {
const settings = vscode.workspace.getConfiguration(ParserContext.ebnfConfigurationName);

var options = new EBNFFormattingOptions();
options.enable = this.settings.get("format.enable");
options.enable = settings.get("format.enable");
options.tabSize = vsOptions.tabSize;
options.insertSpaces = vsOptions.insertSpaces;
options.definingSymbolOnNewLine = this.settings.get("format.definingSymbolOnNewLine");
options.indentDefiningSymbol = this.settings.get("format.indentDefiningSymbol");
options.terminatorSymbolOnNewLine = this.settings.get("format.terminatorSymbolOnNewLine");
options.definitionSeparatorSymbolOnNewLine = this.settings.get("format.definitionSeparatorSymbolOnNewLine");
options.insertSpaceBeforeConcatenateSymbol = this.settings.get("format.insertSpaceBeforeConcatenateSymbol");
options.insertSpaceAtSequenceSymbols = this.settings.get("format.insertSpaceAtSequenceSymbols");
options.defaultDefinitionSeparatorSymbol = this.settings.get("format.defaultDefinitionSeparatorSymbol");
options.defaultOptionSymbols = this.settings.get("format.defaultOptionSymbols");
options.defaultRepeatSymbols = this.settings.get("format.defaultRepeatSymbols");
options.defaultTerminatorSymbol = this.settings.get("format.defaultTerminatorSymbol");
options.definingSymbolOnNewLine = settings.get("format.definingSymbolOnNewLine");
options.indentDefiningSymbol = settings.get("format.indentDefiningSymbol");
options.terminatorSymbolOnNewLine = settings.get("format.terminatorSymbolOnNewLine");
options.definitionSeparatorSymbolOnNewLine = settings.get("format.definitionSeparatorSymbolOnNewLine");
options.insertSpaceBeforeConcatenateSymbol = settings.get("format.insertSpaceBeforeConcatenateSymbol");
options.insertSpaceAtSequenceSymbols = settings.get("format.insertSpaceAtSequenceSymbols");
options.defaultDefinitionSeparatorSymbol = settings.get("format.defaultDefinitionSeparatorSymbol");
options.defaultOptionSymbols = settings.get("format.defaultOptionSymbols");
options.defaultRepeatSymbols = settings.get("format.defaultRepeatSymbols");
options.defaultTerminatorSymbol = settings.get("format.defaultTerminatorSymbol");

return options;
}
Expand Down

0 comments on commit 7f450ce

Please sign in to comment.