Skip to content

Commit

Permalink
Merge pull request #7 from atomiechen/dev
Browse files Browse the repository at this point in the history
Bump version to 0.4.1
  • Loading branch information
atomiechen authored May 29, 2024
2 parents 3a6f8ee + 343abbd commit 8903f9d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to the "vscode-handyllm" extension will be documented in thi
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.


## [0.1.4] - 2024-05-29

### Added

- Foldable frontmatter and messages


## [0.1.3] - 2024-05-22

### Added
Expand Down
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 @@ -13,7 +13,7 @@
"url": "https://github.com/atomiechen/vscode-handyllm"
},
"private": true,
"version": "0.1.3",
"version": "0.1.4",
"engines": {
"vscode": "^1.66.0"
},
Expand Down
10 changes: 5 additions & 5 deletions src/decor_frontmatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import debounce from 'lodash.debounce';

import { isHpromptDoc } from './utils';
import { isFrontmatterBoundary, isHpromptDoc } from './utils';


class FrontmatterConfig {
Expand Down Expand Up @@ -73,12 +73,12 @@ function updateEditor(editor: vscode.TextEditor) {
if (frontmatterConfig.enabled) {
// find frontmatter wrapped in ---
const firstLine = editor.document.lineAt(0);
// check if first line matches ---\s*
if (/^---\s*$/.test(firstLine.text)) {
// find the next ---\s* line
// check if first line matches boundary
if (isFrontmatterBoundary(firstLine.text)) {
// find the next boundary line
for (let i=1; i<editor.document.lineCount; i++) {
const line = editor.document.lineAt(i);
if (/^---\s*$/.test(line.text)) {
if (isFrontmatterBoundary(line.text)) {
const start = new vscode.Position(0, 0);
const end = new vscode.Position(i, line.range.end.character);
frontmatterRanges.push(new vscode.Range(start, end));
Expand Down
5 changes: 2 additions & 3 deletions src/decor_message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import debounce from 'lodash.debounce';
import * as vscode from 'vscode';

import { isHpromptDoc } from './utils';
import { isHpromptDoc, matchRole } from './utils';


class MessageConfig {
Expand Down Expand Up @@ -75,10 +75,9 @@ function updateEditor(editor: vscode.TextEditor) {
const frontmatterRanges: vscode.Range[] = [];

if (messageConfig.enabled) {
// find lines that matches \$\w+\$[^\S\r\n]*({[^{}]*?})?[^\S\r\n]*$
for (let i = 0; i < editor.document.lineCount; i++) {
const text = editor.document.lineAt(i).text;
const match = text.match(/^\$\w+\$[^\S\r\n]*({[^{}]*?})?[^\S\r\n]*$/);
const match = matchRole(text);
if (match) {
const start = new vscode.Position(i, 0);
const end = new vscode.Position(i, text.length);
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { registerCommandCreate } from './cmd_create';
import { registerCommandRun } from './cmd_run';
import { activateFrontmatterDecor } from './decor_frontmatter';
import { activateMessageDecor } from './decor_message';
import { registerFoldingMessage } from './fold_message';
import { registerFoldingFrontmatter } from './fold_frontmatter';


// This method is called when your extension is activated
Expand All @@ -17,6 +19,8 @@ export function activate(context: vscode.ExtensionContext) {

registerCommandCreate(context);
registerCommandRun(context);
registerFoldingMessage(context);
registerFoldingFrontmatter(context);
activateFrontmatterDecor(context);
activateMessageDecor(context);
}
Expand Down
31 changes: 31 additions & 0 deletions src/fold_frontmatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as vscode from 'vscode';
import { isFrontmatterBoundary } from './utils';


export function registerFoldingFrontmatter(context: vscode.ExtensionContext) {
const disposableFoldingFrontmatter = vscode.languages.registerFoldingRangeProvider({ language: 'hprompt' }, {
provideFoldingRanges(document: vscode.TextDocument): vscode.FoldingRange[] {
const folds: vscode.FoldingRange[] = [];
// find frontmatter wrapped in ---
const firstLine = document.lineAt(0);
let end = -1;
if (isFrontmatterBoundary(firstLine.text)) {
for (let i = 1; i < document.lineCount; i++) {
const text = document.lineAt(i).text;
if (isFrontmatterBoundary(text)) {
folds.push(new vscode.FoldingRange(0, i - 1));
end = i;
break;
}
}
if (end === -1) {
folds.push(new vscode.FoldingRange(0, document.lineCount - 1));
}
}
return folds;
}
});

context.subscriptions.push(disposableFoldingFrontmatter);
}

29 changes: 29 additions & 0 deletions src/fold_message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as vscode from 'vscode';
import { matchRole } from './utils';


export function registerFoldingMessage(context: vscode.ExtensionContext) {
const disposableFoldingMessage = vscode.languages.registerFoldingRangeProvider({ language: 'hprompt' }, {
provideFoldingRanges(document: vscode.TextDocument): vscode.FoldingRange[] {
const folds: vscode.FoldingRange[] = [];
let start = -1;
for (let i = 0; i < document.lineCount; i++) {
const text = document.lineAt(i).text;
const match = matchRole(text);
if (match) {
if (start !== -1) {
folds.push(new vscode.FoldingRange(start, i - 1));
}
start = i;
}
}
if (start !== -1) {
folds.push(new vscode.FoldingRange(start, document.lineCount - 1));
}
return folds;
}
});

context.subscriptions.push(disposableFoldingMessage);
}

9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ export function isHpromptDoc(doc?: vscode.TextDocument) {
return isLanguageDoc(hpromptLanguageId, doc);
}

export function matchRole(text: string) {
// find lines that matches roles: \$\w+\$[^\S\r\n]*({[^{}]*?})?[^\S\r\n]*$
return text.match(/^\$\w+\$[^\S\r\n]*({[^{}]*?})?[^\S\r\n]*$/);
}

export function isFrontmatterBoundary(text: string) {
// check if text matches ---\s*
return /^---\s*$/.test(text);
}

0 comments on commit 8903f9d

Please sign in to comment.