I have an array with paths to files in the workspace.
I would like to execute a command that opens all of these files. Which command do I use?
Here is an example of an array with my paths:
["/c:/Users/User/Desktop/Kod/extension/src/home.ts", /c:/Users/User/Desktop/Kod/extension/src/main.ts]
I send these paths from a webview that executes this function:
webviewView.webview.onDidReceiveMessage(async (data) => {
const { tabPaths, name, isOpen } = data.value;
switch (data.type) {
case "onOpenTabs": {
if (!data.value || !tabPaths) {
return;
}
tabPaths.forEach(path => {
vscode.commands.executeCommand("vscode.open", path);
});
break;
}
});
I can confirm that I get the paths in my onDidReceiveMessages.
I then try the vscode.open command but I get this error message:
Is it the wrong command? Can I do something else?
Ah! ok. Thanks to Mark and rioV8 I know the answer. I didn't save a valid textdocument.
Instead I just save an array of vscode.workspace.textDocuments as is. Like so:
const paths = vscode.workspace.textDocuments.map(
(doc) => doc
);
Then open them with vscode.window.showTextDocument instead of vscode.commands.executeCommand. Like so:
paths.forEach(doc => {
vscode.window.showTextDocument(doc);
});
So simple and it works. Thanks.
Related
at the moment, the issue is that Facebook fbclid queries look atrocious on my otherwise very nice URLs.
I WANT THEM GONE
solution below
the solution that i could find was to use a handle hook to remove it if it's there.
import type { Handle } from '#sveltejs/kit';
export const handle: Handle = ({ event, resolve }) => {
let assign: any = {};
if (event.request.url.match(/\?fbclid=.+/)) {
assign = {
redirected: true,
headers: {
Location: event.request.url.replace(/[\?&]fbclid=[^&]+/, '')
}
};
}
let r = resolve(event);
Object.assign(r, assign);
return r;
};
the key part of this code here is the /[\\?&]fbclid=[^&]+/ which allows other query parameters to exist, this will only remove the fbclid parameter as shown on regexr
I hope this helps :)
I'm building vscode extension which generates files with some content.
What i'm trying to do, is force vscode to format those newly created files with rules defined in .editorconfig.
I have tried to do the following:
fs.writeFile(file.name, file.content, (err) => {
if (err) { errors.push(err.message) }
let edit: vscode.WorkspaceEdit = new vscode.WorkspaceEdit();
const { activeTextEditor } = vscode.window;
const fileUri = vscode.Uri.file(file.name);
vscode.workspace.openTextDocument(fileUri).then((td) => {
vscode.commands.executeCommand('vscode.executeFormatDocumentProvider', fileUri, { tabSize: 50, insertSpaces: true, insertFinalNewline: true }).then((edits: vscode.TextEdit[]) => {
if (edits !== undefined) {
let formatEdit = new vscode.WorkspaceEdit();
formatEdit.set(fileUri, edits);
vscode.workspace.applyEdit(formatEdit);
vscode.workspace.saveAll();
}
},
error => console.error(error));
Unfortunately i have faced two issues:
i don't know how to retrieve .editorconfig properties since vscode.workspace.getConfiguration('editor') is not the same as defined in .editorconfig file
This approach not always work sometimes format isn't applied at all
Does anyone know how to solve it?
Following on from this question I have another question about how to implement the provideTasks method of the registerTaskProvider.
Using the npm extension as an example I have tried to implement a basic function, to simply return a single, hard-coded, task. This would then be extended to actually parse a file, ready to add dynamic tasks. However, I have been unable to get this to work.
The code that I am trying is:
vscode.workspace.findFiles('**/*.cake').then((files) => {
if (files.length === 0) {
return emptyTasks;
}
try {
const result: vscode.Task[] = [];
result.push(new vscode.Task({type: 'cake', script: 'NuGet-Restore'} as CakeTaskDefinition, 'Nuget-Restore', 'cake', new vscode.ShellExecution('npm install'), []));
console.log(result);
return Promise.resolve(result);
} catch (e) {
return Promise.resolve(emptyTasks);
}
});
Even though I can see that result contains a Task, I don't see it populated in the Task drop down list.
Can anyone offer any help in why this is not working?
A repository with the current code can be found here.
UPDATE
I have just edited the above code to be the following:
try {
const result: vscode.Task[] = [];
result.push(new vscode.Task({ type: 'cake', script: 'NuGet-Restore' } as CakeTaskDefinition, 'Nuget-Restore', 'cake', new vscode.ShellExecution('npm install'), []));
console.log(result);
return Promise.resolve(result);
} catch (e) {
return Promise.resolve(emptyTasks);
}
By not including the initial findFiles function, it correctly populates the Task Menu with the single hard-coded Task. Why is it that I can't do the return from within the findFiles method? I now suspect that this is a TypeScript/JavaScript problem, rather than one with the provideTasks function, but I am still looking for some help on this.
What is the recommended approach for what I am trying to do? Thanks in advance!
You need to return findFiles to ensure the tasks are actually returned from the task provider:
return vscode.workspace.findFiles('**/*.cake').then((files) => {
...
});
Using "noImplicitReturns": true in your tsconfig.json—or, even better, "strict": true—can help catch bugs like this
I'm attempting to write a plugin for babel, and am needing the filename of the current file that is being parsed. I know the lines of the code are passed in, but I haven't managed to find a reference to the filename. Any help??
For instance given this code what could I do
export default function({ types: t }) {
return {
visitor: {
Identifier(path) {
// something here??
}
}
};
}
You can you this or use the 2nd parameter in a visitor (state)
Identifier(path, state) {
console.log(state.file.opts.filename);
}
For any future viewers, you can use this.file.opts.filename in a visitor function
I want custom code to be made on onblur of first_name field in sugarcrm.
The code should also be upgrade safe.
Please help!
Copy modules/Contacts/metadata/editviewdefs.php
into
custom/modules/Contacts/metadata/editviewdefs.php
(if it does not already exist. If so, use the existing one)
All your changes in this file are upgrade safe. Now open your new file, and you'll see one big array containing everything that's in the EditView of the Contacts-module.
Add the following inside the "templateMeta" array, for instance, right after "form".
'includes'=> array(
array('file'=>'custom/modules/Contacts/EditView.js'),
),
This includes the file custom/modules/Contacts/EditView.js, in which you are free to write all the javascript you feel like!
Remember to do a Quick Repair & Rebuild when you are done.
I don't know which version of SugarCRM you uses, but in SugarCRM 7, the following works:
Create a file 'record.js' in /custom/modules/Contacts/clients/base/views/record/. In that file, you can add custom validation.
Some code you could use is:
({
extendsFrom: 'YourModuleRecordView',
initialize: function (options) {
app.error.errorName2Keys['field_error'] = 'This is an error message';
this._super('initialize', [options]);
this.model.addValidationTask('check_field', _.bind(this._doValidateField, this));
},
_doValidateField: function(fields, errors, callback) {
if (this.model.get('myField') .... ) {
errors['myField'] = errors['myField'] || {};
errors['myField'].field_error = true;
}
callback(null, fields, errors);
}
});
Don't forget to change the fields names like you named them!
This result is only for edit mode. To add this validation to the creation mode, add the file 'create_actions.js' to /custom/modules/Contacts/clients/base/views/create_actions/
Enter the folling code in your 'create_actions.js':
({
extendsFrom: 'CreateActionsView',
initialize: function (options) {
app.error.errorName2Keys['field_error'] = 'Thsis is an error message';
this._super('initialize', [options]);
this.model.addValidationTask('check_field', _.bind(this._doValidateField, this));
},
_doValidateField: function(fields, errors, callback) {
if (.....) {
errors['myField'] = errors['myField'] || {};
errors['myField'].field_error = true;
}
callback(null, fields, errors);
}
});
Perform a repair/rebuild when you added this files with the right code.
You can customize this code to your own needs.