Skip to content

Commit

Permalink
Put some comments and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
whizsid committed Jun 29, 2019
1 parent f372002 commit 8d3ad82
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
43 changes: 28 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import * as vscode from 'vscode';
import { FoundSelection, CommandCollection, ModCheerio, ModCheerioElement } from './types';
const cheerio = require('cheerio');

// Selection states
let selections:FoundSelection[] = [];
let currentSelections =[0];
let foundSelections = false;

let statusBarItem: vscode.StatusBarItem;

// All selection decorations
let selectionDecoration:vscode.TextEditorDecorationType;
let currentSelectionDecoration:vscode.TextEditorDecorationType;
setDecorations();

const cheerio = require('cheerio');

export function activate(context: vscode.ExtensionContext) {

statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
Expand All @@ -21,9 +22,9 @@ export function activate(context: vscode.ExtensionContext) {
let findCommand = vscode.commands.registerCommand('domqs.find', () => {

const activeTextEditor = vscode.window.activeTextEditor;

// Validating the document
const validated = validateDocument(activeTextEditor);

// If validation fails or can not found an active editor
if(!validated||!activeTextEditor){
return;
}
Expand All @@ -36,23 +37,32 @@ export function activate(context: vscode.ExtensionContext) {
if(typeof value ==='undefined'){
return;
}

if(!value.length){
// If value is empty string with white spaces
if(!value.trim().length){
vscode.window.showErrorMessage("Please enter a valid query selector to search.");
return;
}

// Getting all text in current active text document
const document = activeTextEditor.document.getText();

/* Loading our docuement with cheerio. We are using withStartIndices
and withEndIndices options. Official cheerio documentation is not
mention about them anywhere. Because these options is for domhandler.
They are using domhandler for parsing our document to objects. See
https://github.com/fb55/DomHandler#option-withstartindices This is the
official documentation of withStartIndices and withEndIndices properties.
*/
const $ = cheerio.load(document,{ withStartIndices: true,withEndIndices: true, xmlMode:true });

// Setting all selections to an empty array
selections = [];

try {

/* Passing our query selector and retrieving matching DOM objects. Cheerio
is throwing an error when user passed an invalid query selector. this
is why we are wrap this block with a try catch
*/
let matched:ModCheerio = $(value);


// Adding all matched objects to selections
matched.each(function(this: ModCheerio ,i,elem){
selections.push({
start:elem.startIndex,
Expand All @@ -61,9 +71,9 @@ export function activate(context: vscode.ExtensionContext) {
});

if(selections.length){

// Setting current selected object to first object that we found
currentSelections = [0];

// Decorating our selections
decorateSelections(activeTextEditor);
}

Expand All @@ -80,6 +90,9 @@ export function activate(context: vscode.ExtensionContext) {
});
context.subscriptions.push(findCommand);

/* We are storing all commands in an object. Because we don't need to
repeat over and over our codes.
*/
let selectionCommands:CommandCollection = {
selectAll:()=>{

Expand Down Expand Up @@ -262,7 +275,7 @@ function updateStatusBarItem(foundCount:number): void {
* @param document document text as tring
* @param position text offset from the begining
*/
function makePosition(document:string,position:number):vscode.Position{
export function makePosition(document:string,position:number):vscode.Position{
const lines = document.slice(0,position).split(/\r?\n|\r/g);
const lineIndex = lines.length-1;
const line = lines.pop();
Expand All @@ -276,7 +289,7 @@ function makePosition(document:string,position:number):vscode.Position{
* @param document
* @param selection
*/
function makeRange(document:string,selection:FoundSelection):vscode.Range{
export function makeRange(document:string,selection:FoundSelection):vscode.Range{

return new vscode.Range(makePosition(document,selection.start),makePosition(document,selection.end));
}
Expand Down
21 changes: 18 additions & 3 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

// The module 'assert' provides assertion methods from node
import * as assert from 'assert';
// import * as vscode from 'vscode';
import * as myExtension from '../extension';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
Expand All @@ -15,8 +17,21 @@ import * as assert from 'assert';
suite("Extension Tests", function () {

// Defines a Mocha unit test
test("Something 1", function() {
assert.equal(-1, [1, 2, 3].indexOf(5));
assert.equal(-1, [1, 2, 3].indexOf(0));
test("Testing make position", function() {

const position = myExtension.makePosition('<div> \r <span>kjmkjmk</span> \r </div>',8);

assert.equal(1,position.line);
assert.equal(1,position.character);

});

test("Testing make range",function(){
const range = myExtension.makeRange('<div> \r <span>kjmkjmk</span> \n </div>',{start:8,end:33});

assert.equal(1,range.start.line);
assert.equal(1,range.start.character);
assert.equal(2,range.end.line);
assert.equal(3,range.end.character);
});
});
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export interface CommandCollection {
[key:string]:()=>void;
}

/* I have modified cheerio type definitions. Because
cheerio is not supporting domhandler types currently.
*/
export interface ModCheerioElement extends CheerioElement {
startIndex:number;
endIndex:number;
Expand Down

0 comments on commit 8d3ad82

Please sign in to comment.