Skip to content

Commit 09480de

Browse files
committed
Initial port to new monolithic sourcelib package
1 parent 1630a16 commit 09480de

12 files changed

Lines changed: 77 additions & 105 deletions

package-lock.json

Lines changed: 17 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,7 @@
439439
"typescript": "^6.0.3"
440440
},
441441
"dependencies": {
442-
"@sourcelib/captions": "^0.1.1",
443-
"@sourcelib/fs": "^0.1.1",
444-
"@sourcelib/kv": "^0.9.1",
445-
"@sourcelib/vmt": "^0.4.0",
446-
"list-files-in-dir": "^0.1.5"
442+
"list-files-in-dir": "^0.1.5",
443+
"sourcelib": "^0.1.1"
447444
}
448445
}

src/compiler/compiler-base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import vscode from "vscode";
2-
import { isWhitespace } from "@sourcelib/kv/";
2+
import * as sourcelib from "sourcelib";
33
import * as main from "../main";
44
import * as fs from "fs";
55
import { execFile } from "child_process";
@@ -33,7 +33,7 @@ export async function compileSomething(settings: CompileSettings): Promise<void>
3333
filePath = filePath.toLowerCase();
3434
workDir = workDir.toLowerCase();
3535
}
36-
if( exePath == null || isWhitespace(exePath) ) {
36+
if( exePath == null || sourcelib.kv.isWhitespace(exePath) ) {
3737
vscode.window.showErrorMessage(`${settings.compilerName} path is empty. Please configure!`);
3838
return;
3939
}

src/language/CaptionsColorsProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import vscode from "vscode";
22
import KvDocument from "./KvDocument";
3-
import { populateColorTagMatches, ClrTagInfo } from "@sourcelib/captions";
3+
import * as sourcelib from "sourcelib";
44

55
export class CaptionsColorsProvider implements vscode.DocumentColorProvider {
66

@@ -22,7 +22,7 @@ export class CaptionsColorsProvider implements vscode.DocumentColorProvider {
2222
if (kv == null || kv.values.length === 0)
2323
continue;
2424

25-
const clrInfo: ClrTagInfo[] = populateColorTagMatches(kv.value.content);
25+
const clrInfo: sourcelib.captions.ClrTagInfo[] = sourcelib.captions.populateColorTagMatches(kv.value.content);
2626
clrInfo.forEach((clr) => {
2727
const colorInfo = new vscode.ColorInformation(kv.value.range.with(kv.value.range.start.translate(0, clr.start), kv.value.range.start.translate(0, clr.end)),
2828
new vscode.Color(clr.color.r / 255, clr.color.g / 255, clr.color.b / 255, 1.0));

src/language/KvDocument.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import * as vscode from "vscode";
22
import { KvPair, KvPiece } from "../Kv";
3-
import { tokenize, TokenList, Token, TokenType } from "@sourcelib/kv";
3+
import * as sourcelib from "sourcelib";
44
import { KvTokensProviderBase } from "./KvTokensProviderBase";
55

66
export default class KvDocument {
77

88
protected _document: vscode.TextDocument;
9-
protected _tokens: TokenList;
9+
protected _tokens: sourcelib.kv.TokenList;
1010

1111
public get document(): vscode.TextDocument {
1212
return this._document;
1313
}
1414

15-
public get tokens(): TokenList {
15+
public get tokens(): sourcelib.kv.TokenList {
1616
return this._tokens;
1717
}
1818

1919
public static from(document: vscode.TextDocument): KvDocument {
20-
return new KvDocument(document, tokenize(document.getText()));
20+
return new KvDocument(document, sourcelib.kv.tokenize(document.getText()));
2121
}
2222

2323
public static tokenLegend = new vscode.SemanticTokensLegend([
@@ -36,7 +36,7 @@ export default class KvDocument {
3636
"readonly"
3737
]);
3838

39-
private constructor(document: vscode.TextDocument, tks: TokenList) {
39+
private constructor(document: vscode.TextDocument, tks: sourcelib.kv.TokenList) {
4040
this._document = document;
4141
this._tokens = tks;
4242
}
@@ -56,9 +56,9 @@ export default class KvDocument {
5656
const valuePieces: KvPiece[] = [];
5757
for (const token of tokens) {
5858
switch (token.type) {
59-
case TokenType.Key:
59+
case sourcelib.kv.TokenType.Key:
6060
keyPiece = this.getUnquotedToken(token); break;
61-
case TokenType.Value:
61+
case sourcelib.kv.TokenType.Value:
6262
valuePieces.push(this.getUnquotedToken(token)); break;
6363
}
6464
}
@@ -69,13 +69,13 @@ export default class KvDocument {
6969
return new KvPair(keyPiece, valuePieces);
7070
}
7171

72-
public getTokenRange(token: Token): vscode.Range {
72+
public getTokenRange(token: sourcelib.kv.Token): vscode.Range {
7373
const start = new vscode.Position(token.line, token.range.getStart());
7474
const end = new vscode.Position(token.line, token.range.getEnd());
7575
return new vscode.Range(start, end);
7676
}
7777

78-
private getUnquotedToken(token: Token): KvPiece {
78+
private getUnquotedToken(token: sourcelib.kv.Token): KvPiece {
7979
const range = this.getTokenRange(token);
8080
return KvTokensProviderBase.unquoteToken(token, range);
8181
}

src/language/KvFormatter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
/*
44
import vscode from "vscode";
5-
import { } from "@sourcelib/kv";
65
import KvDocument from "./KvDocument";
76
import * as main from "../main";
87

src/language/KvTokensProviderBase.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from "vscode";
2+
import * as sourcelib from "sourcelib";
23
import { KvPiece } from "../Kv";
3-
import { isQuoted, stripQuotes, Token, TokenList, TokenType } from "@sourcelib/kv";
44
import KvDocument from "./KvDocument";
55
import { KvSemanticProcessor, KvSemanticProcessorParams } from "./KvSemanticProcessor";
66

@@ -18,14 +18,14 @@ export abstract class KvTokensProviderBase implements vscode.DocumentSemanticTok
1818
diagnostics: vscode.Diagnostic[] = [];
1919
bracketStack = 0;
2020

21-
protected tokens: TokenList;
21+
protected tokens: sourcelib.kv.TokenList;
2222

2323
protected abstract valueProcessors: KvSemanticProcessor[];
2424

2525
protected abstract keyProcessors: KvSemanticProcessor[];
2626

2727
constructor(legend: vscode.SemanticTokensLegend, diagnosticCollection: vscode.DiagnosticCollection) {
28-
this.tokens = new TokenList;
28+
this.tokens = new sourcelib.kv.TokenList;
2929
this.legend = legend;
3030
this.diagnosticCollection = diagnosticCollection;
3131
}
@@ -51,50 +51,50 @@ export abstract class KvTokensProviderBase implements vscode.DocumentSemanticTok
5151
const tokenRange = kvDoc.getTokenRange(token);
5252

5353
// No further processing on comments
54-
if (token.type === TokenType.Comment) {
54+
if (token.type === sourcelib.kv.TokenType.Comment) {
5555
tokensBuilder.push(tokenRange, "comment", []);
5656
continue;
5757
}
5858

59-
if (token.type === TokenType.Key) {
59+
if (token.type === sourcelib.kv.TokenType.Key) {
6060

6161
// Get next token that isn't a comment
6262
const interestingToken = this.getNextInterestingToken(kvDoc.tokens, i);
6363

6464
// We're an object key
65-
if (interestingToken?.token.type === TokenType.ObjectStart) {
65+
if (interestingToken?.token.type === sourcelib.kv.TokenType.ObjectStart) {
6666
tokensBuilder.push(tokenRange, "struct", []);
6767
this.bracketStack++;
68-
currentScope += "." + stripQuotes(token.value.toLowerCase());
68+
currentScope += "." + sourcelib.kv.stripQuotes(token.value.toLowerCase());
6969
continue;
7070
}
7171

7272
this.processKvKey(token, tokenRange, tokensBuilder, kvDoc, currentScope);
7373

7474
// Is this key not followed by a value?
7575
const interestingTokenType = interestingToken?.token.type;
76-
if (interestingTokenType !== TokenType.Value && interestingTokenType !== TokenType.Conditional) {
76+
if (interestingTokenType !== sourcelib.kv.TokenType.Value && interestingTokenType !== sourcelib.kv.TokenType.Conditional) {
7777
this.diagnostics.push(new vscode.Diagnostic(tokenRange, "Expecting value to this key", vscode.DiagnosticSeverity.Error));
7878
}
7979
continue;
8080
}
8181

82-
if (token.type === TokenType.Value) {
82+
if (token.type === sourcelib.kv.TokenType.Value) {
8383
this.processKvValue(token, tokenRange, tokensBuilder, kvDoc, currentScope);
8484
continue;
8585
}
8686

87-
if (token.type === TokenType.ObjectEnd) {
87+
if (token.type === sourcelib.kv.TokenType.ObjectEnd) {
8888
this.bracketStack--;
8989
currentScope = currentScope.substring(0, currentScope.lastIndexOf("."));
9090
continue;
9191
}
9292

93-
if (token.type === TokenType.PreprocessorKey) {
93+
if (token.type === sourcelib.kv.TokenType.PreprocessorKey) {
9494

9595
// Get next token that isn't a comment
9696
const interestingToken = this.getNextInterestingToken(kvDoc.tokens, i);
97-
if (interestingToken?.token.type === TokenType.Value) {
97+
if (interestingToken?.token.type === sourcelib.kv.TokenType.Value) {
9898

9999
const nextTokenRange = kvDoc.getTokenRange(token);
100100

@@ -108,7 +108,7 @@ export abstract class KvTokensProviderBase implements vscode.DocumentSemanticTok
108108
continue;
109109
}
110110

111-
if(token.type === TokenType.Conditional) {
111+
if(token.type === sourcelib.kv.TokenType.Conditional) {
112112
tokensBuilder.push(tokenRange, "keyword", []);
113113
}
114114
}
@@ -123,13 +123,13 @@ export abstract class KvTokensProviderBase implements vscode.DocumentSemanticTok
123123
return tokensBuilder.build();
124124
}
125125

126-
getNextInterestingToken(tokens: TokenList, i: number): { token: Token; offset: number; } | null {
126+
getNextInterestingToken(tokens: sourcelib.kv.TokenList, i: number): { token: sourcelib.kv.Token; offset: number; } | null {
127127
let n = 1;
128128
if(tokens.length - 1 < i + n) {
129129
return null;
130130
}
131131
let nextToken = tokens[i + n];
132-
while (nextToken.type === TokenType.Comment) {
132+
while (nextToken.type === sourcelib.kv.TokenType.Comment) {
133133
nextToken = tokens[i + n++];
134134
if (nextToken == null)
135135
break;
@@ -138,18 +138,18 @@ export abstract class KvTokensProviderBase implements vscode.DocumentSemanticTok
138138
return { token: nextToken, offset: n };
139139
}
140140

141-
protected processKvKey(token: Token, range: vscode.Range, tokensBuilder: vscode.SemanticTokensBuilder, kvDocument: KvDocument, scope: string): void {
141+
protected processKvKey(token: sourcelib.kv.Token, range: vscode.Range, tokensBuilder: vscode.SemanticTokensBuilder, kvDocument: KvDocument, scope: string): void {
142142
const processed = this.processString(token, range, tokensBuilder, this.keyProcessors, kvDocument, scope);
143143
if (!processed)
144144
tokensBuilder.push(range, "variable", ["declaration"]);
145145
}
146-
protected processKvValue(token: Token, range: vscode.Range, tokensBuilder: vscode.SemanticTokensBuilder, kvDocument: KvDocument, scope: string): void {
146+
protected processKvValue(token: sourcelib.kv.Token, range: vscode.Range, tokensBuilder: vscode.SemanticTokensBuilder, kvDocument: KvDocument, scope: string): void {
147147
const processed = this.processString(token, range, tokensBuilder, this.valueProcessors, kvDocument, scope);
148148
if (!processed)
149149
tokensBuilder.push(range, "string", []);
150150
}
151151

152-
processString(token: Token, range: vscode.Range, tokensBuilder: vscode.SemanticTokensBuilder, processors: KvSemanticProcessor[], kvDocument: KvDocument, scope: string): boolean {
152+
processString(token: sourcelib.kv.Token, range: vscode.Range, tokensBuilder: vscode.SemanticTokensBuilder, processors: KvSemanticProcessor[], kvDocument: KvDocument, scope: string): boolean {
153153
const unquoted = KvTokensProviderBase.unquoteToken(token, range);
154154

155155
const processed = processors.some(processor => {
@@ -170,15 +170,15 @@ export abstract class KvTokensProviderBase implements vscode.DocumentSemanticTok
170170
return processed;
171171
}
172172

173-
protected disallowDuplicate(scopedKey: string, depth: number, token: Token): boolean {
173+
protected disallowDuplicate(scopedKey: string, depth: number, token: sourcelib.kv.Token): boolean {
174174
return false; // Duplicate keys are allowed by default.
175175
}
176176

177-
public static unquoteToken(token: Token, range: vscode.Range): KvPiece {
177+
public static unquoteToken(token: sourcelib.kv.Token, range: vscode.Range): KvPiece {
178178
// Quote tokens
179179
let unquotedContent: string = token.value;
180180
let unquotedRange: vscode.Range = range;
181-
if (isQuoted(token.value)) {
181+
if (sourcelib.kv.isQuoted(token.value)) {
182182
unquotedContent = token.value.substring(1, token.value.length - 1);
183183
unquotedRange = new vscode.Range(range.start.translate(0, 1), range.end.translate(0, -1));
184184
}

src/language/LangKv.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
// ==========================================================================
2-
// Purpose:
3-
// Implementations of language utility providers for the keyvalues language.
4-
//
51
// This is NOT a base for other formats!
6-
// ==========================================================================
72

83
import * as shared from "./Shared";
94
import * as vscode from "vscode";
105
import { KvTokensProviderBase } from "./KvTokensProviderBase";
11-
//import { KvDocumentFormatter } from "./KvFormatter";
126
import { KvSemanticProcessor, KvSemanticProcessorParams } from "./KvSemanticProcessor";
137
import KvDocument from "./KvDocument";
14-
import { matrixRegExp } from "@sourcelib/vmt";
8+
import * as sourcelib from "sourcelib";
159

1610
export const selectorAll: ReadonlyArray<vscode.DocumentFilter> = [ shared.filterKvSaved,
1711
shared.filterKvUnsaved,
@@ -32,7 +26,7 @@ export class KeyvalueSemanticTokensProvider extends KvTokensProviderBase {
3226
protected valueProcessors: KvSemanticProcessor[] =
3327
[
3428
{ regex: /^-?\d+(\.\d+)?$/, processor: this.processValueNumber },
35-
{ regex: matrixRegExp, processor: this.processValueArray }
29+
{ regex: sourcelib.kv.matrixRegExp, processor: this.processValueArray }
3630
];
3731

3832
constructor() {

0 commit comments

Comments
 (0)