Skip to content

Commit

Permalink
Restructured and documented src files for easier maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
willumz committed May 1, 2021
1 parent 44bf639 commit d4f259d
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 213 deletions.
124 changes: 124 additions & 0 deletions out/DocumentSemanticTokensProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentSemanticTokensProvider = void 0;
const vscode = __importStar(require("vscode"));
const tokenTypes_1 = require("./tokenTypes");
// For testing purposes
const index = {
keyword: ["test", "testB"],
};
/**
* Deals with processing the document for the tokens to highlight
*/
class DocumentSemanticTokensProvider {
async provideDocumentSemanticTokens(document, token) {
var documentText = document.getText();
var tokens = this.extractTokens(this.cleanText(documentText));
const builder = new vscode.SemanticTokensBuilder();
tokens.forEach(val => {
builder.push(val.line, val.startCharacter, val.length, val.type);
});
return builder.build();
}
/**
* Extracts tokens from the contents of the file
* @param text - The contents of the open file (should be cleaned first using {@link cleanText})
* @returns The list of processed tokens
*/
extractTokens(text) {
var tokens = [];
var lines = text.split("\n");
for (var i = 0; i < lines.length; i++) {
var re = /([A-Z]|[a-z])+/g;
var matches = lines[i].matchAll(re);
var match = matches.next();
while (!match.done) {
//console.log(match.value);
var matchType = this.determineType(match.value[0]);
if (matchType !== -1 && match.value.index !== undefined)
tokens.push({
line: i,
startCharacter: match.value.index,
length: match.value[0].length,
type: matchType,
});
match = matches.next();
}
}
return tokens;
}
/**
* Removes string literals
*
* Replaces the contents of strings (including the '"' but excluding any instances of '\n' or '\r') with '#'
* @param text - The text to be cleaned
* @returns The cleaned text
*/
cleanText(text) {
var cleaned = "";
var inString = false;
var isEscaped = false;
for (var i = 0; i < text.length; i++) {
var skip = false;
if (text[i] === '"' && !isEscaped) {
inString = !inString;
skip = true;
}
if (text[i] === "\\" && inString)
isEscaped = !isEscaped;
if (text[i] !== "\\")
isEscaped = false;
// console.log(
// `text[i]:${text[i]} isEscaped:${isEscaped} inString:${inString} skip:${skip}`
// );
if (inString || skip) {
if (text[i] === "\n" || text[i] === "\r")
cleaned += text[i];
else
cleaned += "#";
continue;
}
cleaned += text[i];
}
return cleaned;
}
/**
* Determines the type of the token so it can be correctly highlighted
* @param tokenText - the value of the token e.g. `if`
* @returns The numerical type of the token
*/
determineType(tokenText) {
var typeNum = -1;
console.log(tokenText);
Object.keys(index).forEach(typeVal => {
if (index[typeVal].includes(tokenText)) {
console.log("yes");
var code = tokenTypes_1.tokenCodes.get(typeVal);
console.log(code);
if (code !== undefined)
typeNum = code;
}
});
return typeNum;
}
}
exports.DocumentSemanticTokensProvider = DocumentSemanticTokensProvider;
99 changes: 7 additions & 92 deletions out/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,98 +21,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.activate = void 0;
const vscode = __importStar(require("vscode"));
const index = {
"keyword": [
"test",
"test3"
]
};
const tokenCodes = new Map();
tokenCodes.set("keyword", 0);
const tokenTypesLegend = ["keyword"];
const tokenModifiersLegend = [];
const legend = new vscode.SemanticTokensLegend(tokenTypesLegend, tokenModifiersLegend);
const DocumentSemanticTokensProvider_1 = require("./DocumentSemanticTokensProvider");
const tokenTypes_1 = require("./tokenTypes");
/** Entry point for the extension which runs when a file with the language
* type "pseudocode" is opened
*/
function activate(context) {
context.subscriptions.push(vscode.languages.registerDocumentSemanticTokensProvider({ language: "pseudocode" }, new DocumentSemanticTokensProvider(), legend));
const legend = new vscode.SemanticTokensLegend(tokenTypes_1.tokenTypesLegend, tokenTypes_1.tokenModifiersLegend);
context.subscriptions.push(vscode.languages.registerDocumentSemanticTokensProvider({ language: "pseudocode" }, new DocumentSemanticTokensProvider_1.DocumentSemanticTokensProvider(), legend));
}
exports.activate = activate;
class DocumentSemanticTokensProvider {
async provideDocumentSemanticTokens(document, token) {
var documentText = document.getText();
var tokens = this.extractTokens(this.cleanText(documentText));
const builder = new vscode.SemanticTokensBuilder();
tokens.forEach(val => {
builder.push(val.line, val.startCharacter, val.length, val.type);
});
return builder.build();
}
// TODO: Extract tokens from cleaned text
extractTokens(text) {
var tokens = [];
var lines = text.split("\n");
for (var i = 0; i < lines.length; i++) {
var re = /([A-Z]|[a-z])+/g;
var matches = lines[i].matchAll(re);
var match = matches.next();
while (!match.done) {
//console.log(match.value);
var matchType = this.determineType(match.value[0]);
if (matchType !== -1 && match.value.index !== undefined)
tokens.push({
line: i,
startCharacter: match.value.index,
length: match.value[0].length,
type: matchType
});
match = matches.next();
}
}
console.log(tokens);
return tokens;
}
// Remove string literals
// Replaces the contents of strings (including the '"' but excluding any instances of '\n' or '\r') with '#'
cleanText(text) {
var cleaned = "";
var inString = false;
var isEscaped = false;
for (var i = 0; i < text.length; i++) {
var skip = false;
if (text[i] === '"' && !isEscaped) {
inString = !inString;
skip = true;
}
if (text[i] === "\\" && inString)
isEscaped = !isEscaped;
if (text[i] !== "\\")
isEscaped = false;
// console.log(
// `text[i]:${text[i]} isEscaped:${isEscaped} inString:${inString} skip:${skip}`
// );
if (inString || skip) {
if (text[i] === "\n" || text[i] === "\r")
cleaned += text[i];
else
cleaned += "#";
continue;
}
cleaned += text[i];
}
return cleaned;
}
determineType(tokenText) {
var typeNum = -1;
console.log(tokenText);
Object.keys(index).forEach(typeVal => {
console.log(typeVal);
if (index[typeVal].includes(tokenText)) {
console.log("yes");
var code = tokenCodes.get(typeVal);
console.log(code);
if (code !== undefined)
typeNum = code;
}
});
return typeNum;
}
}
10 changes: 10 additions & 0 deletions out/tokenTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tokenModifiersLegend = exports.tokenTypesLegend = exports.tokenCodes = void 0;
const tokenCodes = new Map();
exports.tokenCodes = tokenCodes;
tokenCodes.set("keyword", 0);
const tokenTypesLegend = ["keyword"];
exports.tokenTypesLegend = tokenTypesLegend;
const tokenModifiersLegend = [];
exports.tokenModifiersLegend = tokenModifiersLegend;
118 changes: 118 additions & 0 deletions src/DocumentSemanticTokensProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import * as vscode from "vscode";
import { TokenInterface, IndexInterface } from "./interfaces";
import { tokenCodes } from "./tokenTypes";

// For testing purposes
const index: IndexInterface = {
keyword: ["test", "testB"],
};

/**
* Deals with processing the document for the tokens to highlight
*/
export class DocumentSemanticTokensProvider implements vscode.DocumentSemanticTokensProvider {
async provideDocumentSemanticTokens(
document: vscode.TextDocument,
token: vscode.CancellationToken
): Promise<vscode.SemanticTokens> {
var documentText = document.getText();
var tokens = this.extractTokens(this.cleanText(documentText));

const builder = new vscode.SemanticTokensBuilder();
tokens.forEach(val => {
builder.push(val.line, val.startCharacter, val.length, val.type);
});

return builder.build();
}

/**
* Extracts tokens from the contents of the file
* @param text - The contents of the open file (should be cleaned first using {@link cleanText})
* @returns The list of processed tokens
*/
extractTokens(text: string): TokenInterface[] {
var tokens: TokenInterface[] = [];

var lines = text.split("\n");
for (var i = 0; i < lines.length; i++) {
var re = /([A-Z]|[a-z])+/g;
var matches = lines[i].matchAll(re);

var match = matches.next();
while (!match.done) {
//console.log(match.value);
var matchType = this.determineType(match.value[0]);
if (matchType !== -1 && match.value.index !== undefined)
tokens.push({
line: i,
startCharacter: match.value.index,
length: match.value[0].length,
type: matchType,
});
match = matches.next();
}
}

return tokens;
}

/**
* Removes string literals
*
* Replaces the contents of strings (including the '"' but excluding any instances of '\n' or '\r') with '#'
* @param text - The text to be cleaned
* @returns The cleaned text
*/
cleanText(text: string): string {
var cleaned = "";
var inString = false;
var isEscaped = false;

for (var i = 0; i < text.length; i++) {
var skip = false;

if (text[i] === '"' && !isEscaped) {
inString = !inString;
skip = true;
}
if (text[i] === "\\" && inString) isEscaped = !isEscaped;
if (text[i] !== "\\") isEscaped = false;

// console.log(
// `text[i]:${text[i]} isEscaped:${isEscaped} inString:${inString} skip:${skip}`
// );

if (inString || skip) {
if (text[i] === "\n" || text[i] === "\r") cleaned += text[i];
else cleaned += "#";
continue;
}

cleaned += text[i];
}

return cleaned;
}

/**
* Determines the type of the token so it can be correctly highlighted
* @param tokenText - the value of the token e.g. `if`
* @returns The numerical type of the token
*/
determineType(tokenText: string): number {
var typeNum = -1;

console.log(tokenText);
Object.keys(index).forEach(typeVal => {
if (index[typeVal].includes(tokenText)) {
console.log("yes");
var code = tokenCodes.get(typeVal);
console.log(code);
if (code !== undefined) typeNum = code;
}
});

return typeNum;
}
}
Loading

0 comments on commit d4f259d

Please sign in to comment.