Skip to content

Commit

Permalink
Merge branch 'damms005-feature/vite-and-devdb'
Browse files Browse the repository at this point in the history
  • Loading branch information
amir9480 committed Nov 30, 2024
2 parents 5b27fba + 90959b6 commit cc34871
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This extension provides [Laravel](https://laravel.com/) routes, views and ... au
* [Views and variables](#views-and-variables)
* [Configs](#configs)
* [Translations and translation parameters](#translations-and-translation-parameters)
* [Laravel mix function](#laravel-mix-function)
* [Laravel mix and vite functions](#laravel-mix-and-vite-functions)
* [Validation rules](#validation-rules)
* [View sections and stacks](#view-sections-and-stacks)
* [Env](#env)
Expand All @@ -29,7 +29,7 @@ This extension provides [Laravel](https://laravel.com/) routes, views and ... au
![Configs](./images/screenshot-config.gif)
### Translations and translation parameters
![Translations](./images/screenshot-trans.gif)
### Laravel mix function
### Laravel mix and vite functions
![Mix](./images/screenshot-mix.gif)
### Validation rules
![Validation rules](./images/screenshot-validation.gif)
Expand Down Expand Up @@ -189,6 +189,7 @@ Also if you writing any sensitive code in your service providers, disable the ex
* [Laravel Blade Snippets](https://marketplace.visualstudio.com/items?itemName=onecentlin.laravel-blade)
* [Laravel goto view](https://marketplace.visualstudio.com/items?itemName=codingyu.laravel-goto-view)
* [Laravel goto controller](https://marketplace.visualstudio.com/items?itemName=stef-k.laravel-goto-controller)
* [DevDb](https://marketplace.visualstudio.com/items?itemName=damms005.devdb)

## Credits
* [PHP parser](https://github.com/glayzzle/php-parser)
50 changes: 50 additions & 0 deletions src/ViteProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

import * as vscode from 'vscode';
import * as fs from 'fs';
import Helpers from './helpers';

export default class ViteProvider implements vscode.CompletionItemProvider {
private viteAssets: Array<string> = [];

constructor() {
this.loadViteManifest();
setInterval(() => this.loadViteManifest(), 60000);
}

provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken,
context: vscode.CompletionContext
): Array<vscode.CompletionItem> {
const completions: Array<vscode.CompletionItem> = [];
const func = Helpers.parseDocumentFunction(document, position);

if (func === null) {
return completions;
}

if (func && (Helpers.tags.vite.functions.some((fn: string) => func.function.includes(fn)))) {
for (const asset of this.viteAssets) {
const item = new vscode.CompletionItem(asset, vscode.CompletionItemKind.Value);
item.range = document.getWordRangeAtPosition(position, Helpers.wordMatchRegex);
completions.push(item);
}
}

return completions;
}

private loadViteManifest() {
try {
const manifestPath = Helpers.projectPath('public/build/manifest.json');
if (fs.existsSync(manifestPath)) {
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
this.viteAssets = Object.keys(manifest);
}
} catch (exception) {
console.error(exception);
}
}
}
36 changes: 35 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ViewProvider from "./ViewProvider";
import ConfigProvider from './ConfigProvider';
import TranslationProvider from './TranslationProvider';
import MixProvider from './MixProvider';
import ViteProvider from './ViteProvider';
import ValidationProvider from './ValidationProvider';
import EnvProvider from './EnvProvider';
import MiddlewareProvider from './MiddlewareProvider';
Expand Down Expand Up @@ -40,6 +41,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(LANGUAGES, new ConfigProvider, ...TRIGGER_CHARACTERS));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(LANGUAGES, new TranslationProvider, ...TRIGGER_CHARACTERS));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(LANGUAGES, new MixProvider, ...TRIGGER_CHARACTERS));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(LANGUAGES, new ViteProvider(), ...TRIGGER_CHARACTERS));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(LANGUAGES, new ValidationProvider, ...TRIGGER_CHARACTERS.concat(['|'])));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(LANGUAGES, new EnvProvider, ...TRIGGER_CHARACTERS));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(LANGUAGES, new MiddlewareProvider, ...TRIGGER_CHARACTERS));
Expand Down Expand Up @@ -67,7 +69,7 @@ function showWelcomeMessage(context: vscode.ExtensionContext) {
(previousVersionArray[0] < currentVersionArray[0])
)
) {
message = "Laravel Extra Intellisense updated to " + currentVersion + " - New feature: Add blade directives autocomplete.";
message = "Laravel Extra Intellisense updated to " + currentVersion + " - New Feature✨ : Add Vite autocompletion support.";
}
if (message) {
vscode.window.showInformationMessage(message, '⭐️ Star on Github', '🐞 Report Bug')
Expand All @@ -81,6 +83,38 @@ function showWelcomeMessage(context: vscode.ExtensionContext) {
}
});
context.globalState.update('laravel-extra-intellisense-version', currentVersion);
} else {
suggestDevDbExtension(context);
}
}

async function suggestDevDbExtension(context: vscode.ExtensionContext) {
const DEVDB_EXTENSION_ID = 'damms005.devdb';
const RECOMMENDATION_KEY = 'laravel-extra-intellisense-devdb-extension-recommendation';
const isDevDbExtensionInstalled = vscode.extensions.getExtension(DEVDB_EXTENSION_ID) !== undefined;

if (isDevDbExtensionInstalled) {
return;
}

const currentTime = Date.now();
const lastRecommendation = context.globalState.get<number>(RECOMMENDATION_KEY);
const aYearSinceLastRecommendation = lastRecommendation && (((currentTime - lastRecommendation) > 365 * 24 * 60 * 60 * 1000));

if (!lastRecommendation || aYearSinceLastRecommendation) {
const selection = await vscode.window.showInformationMessage(
'Laravel Extra Intellisense Recommendation: Enhance your database workflow with DevDb - a zero-config extension to auto-load and display database records.',
'Get DevDb',
'Not Now'
);

if (selection === 'Get DevDb') {
vscode.commands.executeCommand(
'extension.open',
DEVDB_EXTENSION_ID
);
}

context.globalState.update(RECOMMENDATION_KEY, currentTime);
}
}
1 change: 1 addition & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class Helpers {
static tags:any = {
config: {classes: ['Config'] , functions: ['config']},
mix: {classes: [] , functions: ['mix']},
vite: {classes: [] , functions: ['vite', '@vite']},
route: {classes: ['Route'] , functions: ['route', 'signedRoute']},
trans: {classes: ['Lang'] , functions: ['__', 'trans', '@lang']},
validation: {classes: ['Validator'] , functions: ['validate', 'sometimes', 'rules']},
Expand Down

0 comments on commit cc34871

Please sign in to comment.