Skip to content

Commit

Permalink
Move head detection to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
neild3r committed Mar 24, 2018
1 parent 7352950 commit b8e06bf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
17 changes: 17 additions & 0 deletions src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ export abstract class Block
return context.substr(0, endPos);
}

/**
* Get the header for the class
*
* @returns {string}
*/
public getClassHead():string
{
let text = this.editor.document.getText();
let regex = /\s*(abstract|final)?\s*(class|trait|interface)/gm;
let match = regex.exec(text);
let end = this.editor.document.positionAt(match.index);
let range = new Range(new Position(0, 0), end);
let head = this.editor.document.getText(range);

return head;
}

/**
* Take the value and parse and try to infer its type
*
Expand Down
10 changes: 8 additions & 2 deletions src/block/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { Range, Position } from "vscode";
*/
export default class FunctionBlock extends Block
{

/**
* @inheritdoc
*/
Expand All @@ -28,15 +27,22 @@ export default class FunctionBlock extends Block
let doc = new Doc('Undocumented function');
let argString = this.getEnclosed(params[6], "(", ")");


if (argString != "") {
let head:string;
let args = argString.split(',');

if (TypeUtil.instance.qualifyClassNames) {
head = this.getClassHead();
}

for (let index = 0; index < args.length; index++) {
let arg = args[index];
let parts = arg.match(/^\s*(\?)?\s*([A-Za-z0-9_\\]+)?\s*\&?((?:[.]{3})?\$[A-Za-z0-9_]+)\s*\=?\s*(.*)\s*/m);
var type = '[type]';

if (parts[2] != null) {
parts[2] = TypeUtil.instance.getFullyQualifiedType(parts[2], this.editor.document);
parts[2] = TypeUtil.instance.getFullyQualifiedType(parts[2], head);
}

if (parts[2] != null && parts[1] === '?') {
Expand Down
11 changes: 2 additions & 9 deletions src/util/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,15 @@ export default class TypeUtil {
* we'll need to access the document
*
* @param {string} type
* @param {TextDocument} document
* @param {string} head
* @returns {string}
*/
public getFullyQualifiedType(type:string, document:TextDocument):string
public getFullyQualifiedType(type:string, head:string):string
{
if (!this.qualifyClassNames) {
return type;
}

let text = document.getText();
let regex = /\s*(abstract|final)?\s*(class|trait|interface)/gm;
let match = regex.exec(text);
let end = document.positionAt(match.index);
let range = new Range(new Position(0, 0), end);
let head = document.getText(range);

let useEx = new RegExp("use\\s+([^ ]*?)((?:\\s+as\\s+))?("+type+");", 'gm');
let full = useEx.exec(head);

Expand Down
22 changes: 15 additions & 7 deletions test/TypeUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import * as assert from 'assert';
import TypeUtil from "../src/util/TypeUtil";
import { window, workspace, TextDocument } from 'vscode';
import { window, workspace, TextDocument, Position, TextEditor } from 'vscode';
import Helper from './helpers';
import FunctionBlock from '../src/block/function';

suite("TypeUtil tests: ", () => {
let textDocument:TextDocument;
let editor:TextEditor;
let head:string;

suiteSetup(function(done) {
workspace.openTextDocument(Helper.fixturePath+'namespace.php').then(doc => {
textDocument = doc;
done();
window.showTextDocument(doc).then(textEditor => {
editor = textEditor;
let block = new FunctionBlock(new Position(0, 0), editor);
head = block.getClassHead();
done();
}, error => {
console.log(error);
})
}, error => {
console.log(error);
});
Expand All @@ -18,19 +26,19 @@ suite("TypeUtil tests: ", () => {
test("Ensure typehint is not mismatched", () => {
let type = new TypeUtil;
type.qualifyClassNames = true;
assert.equal(type.getFullyQualifiedType('Example', textDocument), 'Example');
assert.equal(type.getFullyQualifiedType('Example', head), 'Example');
});

test("Fully qualify typehint from namespace", () => {
let type = new TypeUtil;
type.qualifyClassNames = true;
assert.equal(type.getFullyQualifiedType('FilterInterface', textDocument), 'App\\Test\\Model\\FilterInterface');
assert.equal(type.getFullyQualifiedType('FilterInterface', head), 'App\\Test\\Model\\FilterInterface');
});

test("Fully qualify typehint from namespace use with alias", () => {
let type = new TypeUtil;
type.qualifyClassNames = true;
assert.equal(type.getFullyQualifiedType('BaseExample', textDocument), 'App\\Test\\Model\\Example');
assert.equal(type.getFullyQualifiedType('BaseExample', head), 'App\\Test\\Model\\Example');
});

test("With default settings the integer type formatted is integer", () => {
Expand Down

0 comments on commit b8e06bf

Please sign in to comment.