Skip to content

Commit

Permalink
🍻
Browse files Browse the repository at this point in the history
  • Loading branch information
codingyu committed Jan 30, 2018
1 parent 624e957 commit 0a8aa9f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@
"type": "object",
"title": "Laravel goto view configuration",
"properties": {
"laravel_goto_view.quick_click": {
"laravel_goto_view.quickClick": {
"type": "boolean",
"default": true,
"description": "Use 'Ctrl' or 'Alt' + click"
},
"laravel_goto_view.folder": {
"laravel_goto_view.folderTip": {
"type": "boolean",
"default": true,
"description": "Display path name"
},
"laravel_goto_view.folders": {
"type": "object",
"default": {
"default":"/resources/views"
Expand Down
13 changes: 6 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import * as util from './util';
const REG = /(['"])[^'"]*\1/;

export function activate(context: ExtensionContext) {
let config = workspace.getConfiguration('laravel_goto_view');
let hover = languages.registerHoverProvider(['php','blade','laravel-blade'], {
provideHover(document, position, token) {
provideHover(document, position, token) {
let config = workspace.getConfiguration('laravel_goto_view');
let linkRange = document.getWordRangeAtPosition(position, REG);
if(linkRange){
let filePaths = util.getFilePaths(document.getText(linkRange), document);
let workspaceFolder = workspace.getWorkspaceFolder(document.uri);
if(filePaths.length > 0){
let text:string = "";
for (let i in filePaths) {
text += `\`${filePaths[i].name}\` [${workspaceFolder.name + filePaths[i].showPath}](${filePaths[i].fileUri}) \r`;
text += config.folderTip ? `\`${filePaths[i].name}\`` : '';
text += ` [${workspaceFolder.name + filePaths[i].showPath}](${filePaths[i].fileUri}) \r`;
}
return new Hover(new MarkdownString(text));
}
Expand All @@ -27,10 +28,8 @@ export function activate(context: ExtensionContext) {
});
context.subscriptions.push(hover);

if (config.quick_click) {
let link = languages.registerDocumentLinkProvider(['php','blade','laravel-blade'], new LinkProvider());
context.subscriptions.push(link);
}
let link = languages.registerDocumentLinkProvider(['php','blade','laravel-blade'], new LinkProvider());
context.subscriptions.push(link);
}

export function deactivate() {
Expand Down
29 changes: 16 additions & 13 deletions src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ import * as util from './util';

export class LinkProvider implements DocumentLinkProvider {
public provideDocumentLinks(document: TextDocument, token: CancellationToken): ProviderResult<DocumentLink[]> {
let config = workspace.getConfiguration('laravel_goto_view');
let documentLinks = [];
let index = 0;
let reg = /(['"])[^'"]*\1/g;
while (index < document.lineCount) {
let line = document.lineAt(index);
let result = line.text.match(reg);
if (result != null) {
for (let item of result) {
let filePath = util.getFilePath(item, document);
if(filePath != null){
let start = new Position(line.lineNumber, line.text.indexOf(item) + 1);
let end = start.translate(0, item.length - 2);
let documentlink = new DocumentLink(new Range(start, end), Uri.file(filePath));
documentLinks.push(documentlink);
};
if (config.quickClick) {
while (index < document.lineCount) {
let line = document.lineAt(index);
let result = line.text.match(reg);
if (result != null) {
for (let item of result) {
let file = util.getFilePath(item, document);
if(file != null){
let start = new Position(line.lineNumber, line.text.indexOf(item) + 1);
let end = start.translate(0, item.length - 2);
let documentlink = new DocumentLink(new Range(start, end), file.fileUri);
documentLinks.push(documentlink);
};
}
}
index++;
}
index++;
}
return documentLinks;
}
Expand Down
16 changes: 5 additions & 11 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import { workspace, TextDocument, Uri } from 'vscode';
import * as fs from "fs";

export function getFilePath(text:string, document:TextDocument) {
let config = workspace.getConfiguration('laravel_goto_view');
let workspaceFolder = workspace.getWorkspaceFolder(document.uri).uri.fsPath;
let filePath = workspaceFolder + config.folder[0].path + "/" + text.replace(/\./g,'/').replace(/\"|\'/g,'') + ".blade.php";
if(fs.existsSync(filePath)){
return filePath;
}else{
return null;
}
let paths = getFilePaths(text, document);
return paths.length > 0 ? paths[0] : null;
}

export function getFilePaths(text:string, document:TextDocument): any {
Expand All @@ -21,7 +15,7 @@ export function getFilePaths(text:string, document:TextDocument): any {
let result = [];
if (text.indexOf("::") != -1) {
let info = text.split('::');
let showPath = config.folder[info[0]] + "/" + info[1].replace(/\./g,'/') + ".blade.php";
let showPath = config.folders[info[0]] + "/" + info[1].replace(/\./g,'/') + ".blade.php";
let filePath = workspaceFolder + showPath;
if(fs.existsSync(filePath)){
result.push({
Expand All @@ -31,8 +25,8 @@ export function getFilePaths(text:string, document:TextDocument): any {
});
}
} else {
for (let item in config.folder) {
let showPath = config.folder[item] + "/" + text.replace(/\./g,'/') + ".blade.php";
for (let item in config.folders) {
let showPath = config.folders[item] + "/" + text.replace(/\./g,'/') + ".blade.php";
let filePath = workspaceFolder + showPath;
if(fs.existsSync(filePath)){
result.push({
Expand Down

0 comments on commit 0a8aa9f

Please sign in to comment.