Skip to content

Commit

Permalink
Find all references
Browse files Browse the repository at this point in the history
  • Loading branch information
igochkov committed Jul 1, 2023
1 parent 4d04bd6 commit bc642e6
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 28 deletions.
15 changes: 10 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ 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.
- [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.
- [ ] **Find All References / Peek Definition**: Find every reference to a specific symbol in your EBNF code.
- [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.
- [ ] **IntelliSense - List Members**: Get a list of members for a given symbol.
Expand All @@ -25,17 +25,22 @@ 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**

- _Find All References_

### Version 1.2
**Released on 2023-06-22**

- _Go to definition_ feature implemented.
- _Code folding (by markers)_ feature implemented.
- _Go to definition_
- _Code folding (by markers)_
- 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**

- _Rename symbol_ feature implemented.
- _Rename symbol_
- Fixed broken links to images.

### Version 1.0
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ The EBNF Tools extension currently offers the following features:

![Code folding (by markers)](https://raw.githubusercontent.com/igochkov/vscode-ebnf/main/docs/folding.gif)

- **Find All References**: Find every reference to a specific symbol in your EBNF code.

![Find All References](https://raw.githubusercontent.com/igochkov/vscode-ebnf/main/docs/references.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/references.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 31 additions & 23 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
import { DocumentFilter, languages, workspace, TextDocumentChangeEvent, Disposable, Event, TextDocument } from 'vscode'
import { EBNFRenameProvider } from './providers/RenameProvider'
import { DocumentFilter, languages, workspace, TextDocumentChangeEvent, ExtensionContext, TextDocument } from 'vscode';
import { EBNFRenameProvider } from './providers/RenameProvider';
import { EBNFDefinitionProvider } from './providers/DefinitionProvider';
import { EBNFReferenceProvider } from './providers/EBNFReferenceProvider';

const EbnfSelector: DocumentFilter = { language: 'ebnf', scheme: 'file' };
export function activate(ctx: ExtensionContext): void
{
const EbnfSelector: DocumentFilter = { language: 'ebnf', scheme: 'file' };

languages.registerRenameProvider(EbnfSelector, new EBNFRenameProvider());
languages.registerDefinitionProvider(EbnfSelector, new EBNFDefinitionProvider());
ctx.subscriptions.push(languages.registerRenameProvider(EbnfSelector, new EBNFRenameProvider()));
ctx.subscriptions.push(languages.registerDefinitionProvider(EbnfSelector, new EBNFDefinitionProvider()));
ctx.subscriptions.push(languages.registerReferenceProvider(EbnfSelector, new EBNFReferenceProvider()));

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
// workspace.onDidOpenTextDocument(OnDocumentOpen);
// workspace.onDidChangeTextDocument(OnDocumentChange);
// workspace.onDidCloseTextDocument(OnDocumentClose);
}

function OnDocumentChange(event: TextDocumentChangeEvent) {
// TODO: Not implemented yet
// if changes and file is ebnf
// re-parse
}
// 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
}
// function OnDocumentClose(document: TextDocument)
// {
// // TODO: Not implemented yet
// // if the file is ebnf
// // remove from cache
// }
44 changes: 44 additions & 0 deletions src/providers/EBNFReferenceProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as vscode from "vscode";
import { CharStreams, CommonTokenStream } from 'antlr4ts';
import { ParseTreeListener } from 'antlr4ts/tree/ParseTreeListener';

import { EBNFLexer } from '../parser/EBNFLexer';
import { EBNFParser } from '../parser/EBNFParser';
import { ISymbolInfo } from '../ISymbolInfo';
import { IdentifierListener } from '../listeners/IdentifierListener';

export class EBNFReferenceProvider implements vscode.ReferenceProvider
{
public provideReferences(document: vscode.TextDocument, position: vscode.Position, context: vscode.ReferenceContext, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Location[]>
{
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 IdentifierListener();
parser.addParseListener(listener as ParseTreeListener);

parser.syntax();

var result: vscode.Location[]
= listener.symbols.filter(symbol => symbol.text == text)
.map(ref => new vscode.Location(document.uri,
new vscode.Range(
ref.line - 1,
ref.charPositionInLine,
ref.line - 1,
ref.charPositionInLine + ref.text.length
)));

return result;
}
}

0 comments on commit bc642e6

Please sign in to comment.