Skip to content

Commit

Permalink
Go to definition implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
igochkov committed Jun 22, 2023
1 parent 53f1e25 commit 3a956f3
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 10 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Our development roadmap outlines the planned features for EBNF Tools. Features m
- [x] **Commenting blocks of code**: Allows you to quickly comment out blocks of EBNF code.
- [x] **Bracket matching**: Aids in matching brackets in your EBNF code.
- [x] **Rename symbol**: Allows renaming of symbols throughout your EBNF code.
- [x] **Go to Definition**: Quickly navigate to the definition of a symbol in your EBNF code.
- [ ] **Code folding**: Hide sections of your EBNF code for easier reading and navigation.
- [ ] **Go to Definition**: Quickly navigate to the definition of a symbol in your EBNF code.
- [ ] **Find All References / Peek Definition**: 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.
Expand All @@ -26,17 +26,18 @@ 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.2
**Pending Release**
**Released on 2023-06-22**

- Added support for syntax highlighting in Markdown fenced code blocks.
- _Go to definition_ feature implemented.
- Added support for syntax highlighting in _Markdown fenced code blocks_. Big thanks to [Brendan Doney](https://github.com/brdoney) for providing *[codeblock.json](https://github.com/igochkov/vscode-ebnf/blob/main/syntaxes/codeblock.json)*

### Version 1.1
**Released on 2023-06-17**

- Introduced the Rename symbol feature.
- _Rename symbol_ feature implemented.
- Fixed broken links to images.

### Version 1.0
**Released on 2016-09-02**

- Released initial version of the EBNF Tools extension, featuring syntax highlighting, code commenting, and bracket matching.
- Initial version of the EBNF Tools extension, featuring _syntax highlighting_, _code commenting_, and _bracket matching_.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ The EBNF Tools extension currently offers the following features:

![Rename symbol](https://raw.githubusercontent.com/igochkov/vscode-ebnf/main/docs/rename-symbol.gif)

- **Go to Definition and Peek Definition**: Quickly navigate to the definition of a symbol in your EBNF.

![Go to Definition](https://raw.githubusercontent.com/igochkov/vscode-ebnf/main/docs/go-to-definition.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/go-to-definition.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"icon": "images/icon.png",
"engines": {
"vscode": "^1.75.0"
"vscode": "^1.75.1"
},
"license": "MIT",
"categories": [
Expand Down
28 changes: 26 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { DocumentFilter, languages } from 'vscode'
import { DocumentFilter, languages, workspace, TextDocumentChangeEvent, Disposable, Event, TextDocument } from 'vscode'
import { EBNFRenameProvider } from './providers/RenameProvider'
import { EBNFDefinitionProvider } from './providers/DefinitionProvider';

const EbnfSelector: DocumentFilter = { language: 'ebnf', scheme: 'file' };

languages.registerRenameProvider(EbnfSelector, new EBNFRenameProvider());
languages.registerRenameProvider(EbnfSelector, new EBNFRenameProvider());
languages.registerDefinitionProvider(EbnfSelector, new EBNFDefinitionProvider());

workspace.onDidOpenTextDocument(OnDocumentOpen);
workspace.onDidChangeTextDocument(OnDocumentChange);
workspace.onDidCloseTextDocument(OnDocumentClose);

function OnDocumentOpen(document: TextDocument) {
// TODO: Not implemented yet
// if the file is ebnf
// parse and cache AST
}

function OnDocumentChange(event: TextDocumentChangeEvent) {
// TODO: Not implemented yet
// if changes and file is ebnf
// re-parse
}

function OnDocumentClose(document: TextDocument) {
// TODO: Not implemented yet
// if the file is ebnf
// remove from cache
}
15 changes: 15 additions & 0 deletions src/listeners/DefinitionListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Token } from 'antlr4ts';
import { SyntaxRuleContext } from '../parser/EBNFParser';
import { EBNFParserListener } from '../parser/EBNFParserListener';

export class DefinitionListener implements EBNFParserListener {
public definitions: Token[] = [];

exitSyntaxRule(ctx: SyntaxRuleContext) {
const terminalNode = ctx.IDENTIFIER();

if (terminalNode !== undefined) {
this.definitions.push(terminalNode.symbol);
}
}
}
45 changes: 45 additions & 0 deletions src/providers/DefinitionProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { TextDocument, Position, CancellationToken, Range, ProviderResult, DefinitionProvider, TextEdit, LocationLink, Definition } from 'vscode'
import { EBNFLexer } from '../parser/EBNFLexer';
import { CharStreams, CommonTokenStream } from 'antlr4ts';
import { ParseTreeListener } from 'antlr4ts/tree/ParseTreeListener';
import { EBNFParser } from '../parser/EBNFParser';
import { DefinitionListener } from '../listeners/DefinitionListener';

export class EBNFDefinitionProvider implements DefinitionProvider {
public provideDefinition(
document: TextDocument,
position: Position,
_: CancellationToken): ProviderResult<Definition | LocationLink[]> {

const range = document.getWordRangeAtPosition(position)
const text = document.getText(range)

if (!text) {
return;
}

const content = document.getText();
const inputStream = CharStreams.fromString(content);
const lexer = new EBNFLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new EBNFParser(tokenStream);
const listener = new DefinitionListener();
parser.addParseListener(listener as ParseTreeListener);

parser.syntax();

const def = listener.definitions.find(d => d.text === text);

var result: Definition = {
uri: document.uri,
range: new Range(
def.line - 1,
def.charPositionInLine,
def.line - 1,
def.charPositionInLine + def.text.length
)
};

return result;
}
}

0 comments on commit 3a956f3

Please sign in to comment.