Skip to content

Commit

Permalink
add beta version of outline for Lencerf#25
Browse files Browse the repository at this point in the history
  • Loading branch information
isometimescode committed Apr 16, 2021
1 parent 08e6667 commit 5600f7f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* Add/remove diagnostic information when standalone files are opened/closed
* Let formatter ignore non-beancount files

### 0.5.7 (2021-02-06)
* fix npm audit warnings by bumping packages
* (@isometimescode) Add outline for ;#region statements, related to (https://github.com/Lencerf/vscode-beancount/issues/25)

### 0.5.6 (2020-03-18)
* fix [#40](https://github.com/Lencerf/vscode-beancount/issues/40)

Expand Down Expand Up @@ -88,7 +92,7 @@
### 0.2.0 (2018-04-26)

- Code snippets ([@vlamacko](https://github.com/Lencerf/vscode-beancount/pull/7))
- Run Fava to view balances
- Run Fava to view balances

### 0.1.1 (2018-04-22)
- The extension now will not check unrelated files. [#8](https://github.com/Lencerf/vscode-beancount/issues/8)
Expand All @@ -103,4 +107,4 @@
- Made the extension honor editor.tabSize when inserting new postings

### 0.0.1 (2017-03-14)
- Initial release
- Initial release
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ window as the main file.
## Recommended practices

0. **Make sure you installed Python3 and [beancount](https://pypi.org/project/beancount/). Set `beancount.python3Path` to the correct path.**
1. Split your ledger into several `.bean` files according to time and
put all your `open`/`close` in a main file.
2. Include all other files in the
1. Split your ledger into several `.bean` files according to time and
put all your `open`/`close` in a main file.
2. Include all other files in the
main file by the `include` command in the main bean file.
3. Open `BeanFolder` with VSCode and set `beancount.mainBeanFile` to the full path of `main.bean` in the current [Workspace Settings](https://code.visualstudio.com/docs/getstarted/settings).

Expand All @@ -59,7 +59,7 @@ If you open `.vscode/settings.json`, you should see something like this:
{
"beancount.mainBeanFile": "main.bean"
}
```
```

Now once `BeanFolder` is opened as a workspace in VSCode, this extension will be able to invoke beancount to check errors and calculate balances.

Expand All @@ -73,8 +73,12 @@ see GitHub [issue page](https://github.com/Lencerf/vscode-beancount/issues)
* Add/remove diagnostic information when standalone files are opened/closed
* Let formatter ignore non-beancount files

### 0.5.7
* fix npm audit warnings by bumping packages
* (@isometimescode) Add outline for ;#region statements, related to (https://github.com/Lencerf/vscode-beancount/issues/25)

### 0.5.6
* fix [#40](https://github.com/Lencerf/vscode-beancount/issues/40)

### 0.5.5
### 0.5.5
* fix [#38](https://github.com/Lencerf/vscode-beancount/issues/38): account autocompletion fails due to commodity price error
11 changes: 11 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {existsSync} from 'fs';
import {isAbsolute, join} from 'path';
import {FavaManager} from './favaManager';
import {ActionProvider} from './actionProvider';
import {SymbolProvider} from './symbolProvider';
import {Completer} from './completer';
import {Formatter} from './formatter';
import {runCmd} from './utils';
Expand Down Expand Up @@ -52,6 +53,14 @@ export function activate(context: vscode.ExtensionContext) {
extension.formatter.instantFormat(e),
),
);

context.subscriptions.push(
vscode.languages.registerDocumentSymbolProvider(
{ scheme: 'file', language: 'beancount' },
extension.symbolProvider
)
);

context.subscriptions.push(
vscode.workspace.onDidSaveTextDocument((e: vscode.TextDocument) =>
extension.refreshData(context),
Expand Down Expand Up @@ -86,6 +95,7 @@ export function deactivate() { }
export class Extension {
completer: Completer;
actionProvider: ActionProvider;
symbolProvider: SymbolProvider;
favaManager: FavaManager;
diagnosticCollection: vscode.DiagnosticCollection;
formatter: Formatter;
Expand All @@ -97,6 +107,7 @@ export class Extension {
this.context = context;
this.completer = new Completer(this);
this.actionProvider = new ActionProvider();
this.symbolProvider = new SymbolProvider();
this.favaManager = new FavaManager(this);
this.diagnosticCollection = vscode.languages.createDiagnosticCollection(
'Beancount',
Expand Down
45 changes: 45 additions & 0 deletions src/symbolProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as vscode from 'vscode';

export class SymbolProvider implements vscode.DocumentSymbolProvider {
private format(tokens: string[]): string {
const name = [];

for (let j = 0; j < tokens.length; j++) {
const element = tokens[j];
if (
element.startsWith('*') ||
element === ';#region' ||
element === ';#endregion'
) {
continue;
}
name.push(element);
}
return name.join(' ');
}

provideDocumentSymbols(
document: vscode.TextDocument,
token: vscode.CancellationToken
): Promise<vscode.DocumentSymbol[]> {
return new Promise((resolve, reject) => {
const symbols: vscode.DocumentSymbol[] = [];
for (let i = 0; i < document.lineCount; i++) {
const line = document.lineAt(i);

// line can start or end with ;#region
if (line.text.includes(';#region')) {
const symbol = new vscode.DocumentSymbol(
this.format(line.text.split(' ')),
'Region',
vscode.SymbolKind.String,
line.range,
line.range
);
symbols.push(symbol);
}
}
resolve(symbols);
});
}
}

0 comments on commit 5600f7f

Please sign in to comment.