VSCode Open a File In a Specific Line Number Using JS - visual-studio-code

I added a new button to my VSCode, such that when I click it - it compiles the current folder, and shows dialog boxes using vscode.window.showInformationMessage.
Each box shows a compilation error, and has a button in it. Once the user clicks the button - it opens the problematic file in a tab using vscode.workspace.openTextDocument.
I want to make the button to also navigate me to the problematic line in the problematic file.
My question is:
Given a number, is it possible to navigate to a specific line number inside a file?
Sample code of what I achieved so far:
// Bullshit to give some context
const pattern = /(In \w+.jack)/g;
var i = s.search(pattern);
var substring = s.substring(i + 1)
var j = substring.search(pattern);
var s = "bsadsdbla In main.jack (line 55) sqdwqe blasdsd wq qqweq"
let GoToFile = 'Go to File';
var k = s.search(/(\w+.jack)/);
var l = s.search(/(.jack)/)
var fileName = s.substring(k, l);
// ---------> This is the important part <----------------
vscode.window.showInformationMessage(s.substring(i, j), GoToFile).then(selection => {
if (selection === GoToFile) {
vscode.workspace.openTextDocument(currentDirectory + '\\' + fileName + '.jack')
.then(document => vscode.window.showTextDocument(document));
}
});

I am assuming you have some code/regex in place that gives you the line number. Whenever someone clicks your GoToFile method invoke the following code :
activeEditor.selections = [new vscode.Selection(lineToGo, lineToGo)];
var range = new vscode.Range(lineToGo, lineToGo);
activeEditor.revealRange(range);

Some background:
As #rioV8 mentioned, revealRange was the way to go, but the problem was that I couldn't understand how to use it using the VSCode API, so here #Shahriar Hossain came into the picture. #Shahriar Hossain's code works, however, there was an important declaration missing, and I also had to figure out how to run the code when the user clicks the button.
This is the full solution:
vscode.window.showInformationMessage(s.substring(i), GoToFile).then(selection => {
if (selection === GoToFile) {
vscode.workspace.openTextDocument(currentDirectory + '\\' + fileName + '.jack')
.then(document => vscode.window.showTextDocument(document))
// Whatever code inside this "then" block
// will be executed on button click
.then(x => {
let m = s.substring(i, j).search(/\(line \d+\)/);
let subStr = s.substring(m + 6);
let n = subStr.search(/\)/);
subStr = subStr.substring(0, n);
let lineToGo = parseInt(subStr.match(/\d+/));
// The missing declaration of the activeEditor
let activeEditor = vscode.window.activeTextEditor;
let range = activeEditor.document.lineAt(lineToGo - 1).range;
activeEditor.selection = new vscode.Selection(range.start, range.end);
activeEditor.revealRange(range);
})
}
});

Related

How to add and edit a short answer in multiple google forms

So I have multiple google forms (around 20 forms), that I need to do 2 things to them:
1- These 20 forms are placed in a folder in my google drive. I need to add more like an "Access code" where users will have to insert in order to continue the solving the quiz.
The way I did that was to add a "short answer" question to "section 1" of the quiz asking "Enter your Access Code", add "response validation", "Regular expression" and "Pattern". Also making this a "required question". This should look something like the below picture
Example of google form
So is it possible to have a scriptto add this question to all 20 forms
2- The "access code" in these google forms will have to be updated frequently, so I don' want to be updating the "Pattern" manually for each form, is t possible to have a google script to edit the value of the pattern for each form
Thanks in advance guys :)
I managed to solve this issue that I was having, through looking for different codes and here are the codes that I used.
N.B. The codes might not be very clean as I was copying them from other parts/projects, but they have worked for me
1- Update the 20 forms with adding the access code question, I figured it was not possible to add a question at a certain position in the google form, however I can add a question at the end of the form and then move this item to the position I want:
function AddAccesscodeQ() {
var filess = DriveApp.getFolderById("Drive id>").getFiles();
while (filess.hasNext()) {
var file = filess.next();
var form = FormApp.openById(file.getId());
var sectionIndex= 0; // Please set the index you want to insert.
//I added a "sample item" to be moved and edited later
var newItemQ = form.addTextItem().setTitle("New sample item").getIndex(); // New sample item
// I added a Pagebreak that also should be moved after the questions "Enter Your Access Code"
var newItemPB = form.addPageBreakItem().getIndex();
var items = form.getItems(FormApp.ItemType.PAGE_BREAK);
var sections = [0];
for (var i = 0; i < items.length; i++) {
// I pushed the items in the google form twice downwards, to be able to move the "sample item" and "Page break" to the top of the form
sections.push(items[i].getIndex());
sections.push(items[i].getIndex());
}
var insertIndex = sections[sectionIndex + 1] || null;
if (insertIndex) {
// Here I moved the 2 new items to the desired positions
form.moveItem(newItemQ, 0);
form.moveItem(newItemPB, 1);
}
// Here I am going to edit the "Sample Question" to be as desired
var itemss = form.getItems();
var itemID = itemss[0].getId();
var itemse = form.getItemById(itemID).asTextItem()
.setTitle('Enter Your Access Code').setRequired(true);
//Create validation rule
var validation = FormApp.createTextValidation()
.setHelpText('Invalid Code')
.requireTextMatchesPattern("<Access Code>")
.build();
itemse.setValidation(validation);
}
}
2- The second problem was that I later might need to change this access code to a new one for the 20 forms
function UpdateAccessCode() {
var filesPhCH = DriveApp.getFolderById("<Drive ID>").getFiles();
while (filesPhCH.hasNext()) {
var file = filesPhCH.next();
var form = FormApp.openById(file.getId());
var items = form.getItems();
//Loop through the items and list them
for (var i = 0;i<items.length;i++){
var item = items[i];
var itemID = item.getId();
var itemtitle = item.getTitle();
var itemindex = item.getIndex();
// I found no need to continue the for loop since the items that need modification are at the top of the form
if (itemindex == 0){
break;
}
}
//Select the question you want to update
var itemse = form.getItemById(itemID).asTextItem()
.setTitle('Enter Your Access Code');
//Create validation rule
var validation = FormApp.createTextValidation()
//.setTitle('Enter Your Access Code');
.setHelpText('Invalid Code')
.requireTextMatchesPattern("<Enter the new Access Code>")
.build();
itemse.setValidation(validation);
}
}
I hope this might help someone as it has saved a lot of time for me ;)

how to close CrossApplicationNavigation pages from script with button

On the app i'm currently developping,
I have several hyperlink to standard sap backend transaction such as BP, PDL and so on.
i'm opening them by means of CrossApplicationNavigation.
they open new tab.
on my main app, I implemented a button to close every tab and returning to root view.
I tried the following :
Script to close other tabs or browser
here is my code :
var oCrossAppNavigator = sap.ushell.Container.getService("CrossApplicationNavigation");
var oExtUi = oEvent.getSource().getText();
var hash = oCrossAppNavigator.hrefForExternal({
target: {
semanticObject: "ZSIMM_PDL",
action: "display"
},
params: {
"UtilitiesPDL": oExtUi
}
});
var url = window.location.href.split("#")[0] + hash;
this._oComponent = sap.ui.component(sap.ui.core.Component.getOwnerIdFor(this.getView()));
this._myModel = this._oComponent.getModel("oModelWindow");
this._myModel.setProperty("/Window", window);
sap.m.URLHelper.redirect(url, true);
},
this way, i open the new tab with my semanticObject. and I register it in my Model.
the next step is the method linked with my button :
this._myModel = this._oComponent.getModel("oModelWindow");
var aWindow = this._myModel.getData().Window;
aWindow.close();
I'm loading the window of the tab I opened.
the instruction close doesn't work it writes :
"Scripts may close only the windows that were opened by it."
what I understand in CrossApplicationNavigation is that it opened a new tab and redirect by use of sap.m.URLHelper.redirect(url, true);
does the current screen before calling the new one know which page it opens ?
and this way is there a method to close it manually ?
I found a solution which is the following :
when opening the crossApplicationnavigation, I put on memory the tab opened :
var oExtUi = oEvent.getSource().getText();
var hash = oCrossAppNavigator.hrefForExternal({
target: {
semanticObject: "ZSIMM_PDL",
action: "display"
},
params: {
"UtilitiesPDL": oExtUi
}
});
var url = window.location.href.split("#")[0] + hash;
this._oComponent = sap.ui.component(sap.ui.core.Component.getOwnerIdFor(this.getView()));
this._myModel = this._oComponent.getModel("oModelWindow");
var aWindow = this._myModel.getData().Windows;
aWindow.push(window.open(url, "_blank"));
this._myModel.setProperty("/Windows", aWindow);
and in the button method, I recall all tab and close them all this way :
this._oComponent = sap.ui.component(sap.ui.core.Component.getOwnerIdFor(this.getView()));
this._myModel = this._oComponent.getModel("oModelWindow");
//then you can iterate over them and close them all like this:
var oWindow = this._myModel.getData().Windows;
for (var i = 0; i < oWindow.length; i++) {
oWindow[i].close();
}
that way when clicking the button all other tab are closed.

VSCode extension - how to alter file's text

I have an extension that grabs the open file's text and alters it. Once the text is altered, how do I put it back into the file that is displayed in VSCode?
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "myExtension" is now active!');
console.log(process.versions);
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('extension.myExtension', () => {
// The code you place here will be executed every time your command is executed
let activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
return;
}
let text = activeEditor.document.getText();
getAsyncApi(text).then((textToInsertIntoDoc) => {
let finaldoc = insertTextIntoDoc(text, textToInsertIntoDoc);
// not what I want - just used to see new text
vscode.window.showInformationMessage(textToInsertIntoDoc);
});
});
context.subscriptions.push(disposable);
}
The API you can use here is TextEditor.edit, whose definition is
edit(callback: (editBuilder: TextEditorEdit) => void, options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;
It asks for a callback as the first parameter and in the callback, you can make edits to the document by visiting editBuilder.
I put a sample extension in https://github.com/Microsoft/vscode-extension-samples/tree/master/document-editing-sample which reverses the content in current selection, which is basically a simple use TextEditor.edit.
This is a revision of the main function in Rebornix's extension sample (included with the set of Microsoft extension samples) that handles the selection issues you raised. It reverses the content of the selection(s) (leaving the selections) or if a selection is empty it will reverse the word under the cursor at that selection without leaving anything selected. It often makes sense to leave a selection, but you can add code to remove selection.
let disposable = vscode.commands.registerCommand('extension.reverseWord', function () {
// Get the active text editor
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
editor.edit(editBuilder => {
editor.selections.forEach(sel => {
const range = sel.isEmpty ? document.getWordRangeAtPosition(sel.start) || sel : sel;
let word = document.getText(range);
let reversed = word.split('').reverse().join('');
editBuilder.replace(range, reversed);
})
}) // apply the (accumulated) replacement(s) (if multiple cursors/selections)
}
});
Admittedly, while I could remove a single selection by setting .selection to a new empty selection that doesn't seem to work with .selections[i]. But you can make multiple changes without having selections in the first place.
What you don't want to do is make a selection through code just to alter text through code. Users make selections, you don't (unless the end purpose of the function is to make a selection).
I came to this question looking for a way to apply a textEdit[] array (which is normally returned by a provideDocumentRangeFormattingEdits callback function). If you build changes in the array you can apply them to your document in your own function:
const { activeTextEditor } = vscode.window;
if (activeTextEditor) {
const { document } = activeTextEditor;
if (document) {
/*
build your textEdits similarly to the above with insert, delete, replace
but not within an editBuilder arrow function
const textEdits: vscode.TextEdit[] = [];
textEdits.push(vscode.TextEdit.replace(...));
textEdits.push(vscode.TextEdit.insert(...));
*/
const workEdits = new vscode.WorkspaceEdit();
workEdits.set(document.uri, textEdits); // give the edits
vscode.workspace.applyEdit(workEdits); // apply the edits
}
}
So that's another way to apply edits to a document. Even though I got the editBuilder sample to work correctly without selecting text, I have had problems with selections in other cases. WorkspaceEdit doesn't select the changes.
Here is the code snippet that will solve your issue :
activeEditor.edit((selectedText) => {
selectedText.replace(activeEditor.selection, newText);
})
Due to the issues I commented about in the above answer, I ended up writing a quick function that does multi-cursor friendly insert, and if the selection was empty, then it does not leave the inserted text selected afterwards (i.e. it has the same intuitive behavior as if you had pressed CTRL + V, or typed text on the keyboard, etc.)
Invoking it is simple:
// x is the cursor index, it can be safely ignored if you don't need it.
InsertText(x => 'Hello World');
Implementation:
function InsertText(getText: (i:number) => string, i: number = 0, wasEmpty: boolean = false) {
let activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) { return; }
let sels = activeEditor.selections;
if (i > 0 && wasEmpty)
{
sels[i - 1] = new vscode.Selection(sels[i - 1].end, sels[i - 1].end);
activeEditor.selections = sels; // required or the selection updates will be ignored! 😱
}
if (i < 0 || i >= sels.length) { return; }
let isEmpty = sels[i].isEmpty;
activeEditor.edit(edit => edit.replace(sels[i], getText(i))).then(x => {
InsertText(getText, i + 1, isEmpty);
});
}
let strContent = "hello world";
const edit = new vscode.WorkspaceEdit();
edit.insert(YOUR_URI, new vscode.Position(0, 0), strContent);
let success = await vscode.workspace.applyEdit(edit);

Google Apps upload from spreadsheet: cannot reference active cell

I have this google app script which should
show file upload dialog
store file in google drive
write the url of the file into the current cell
All goes well except step 3, where the cell updated is always cell A1 in the first sheet. But the cursor is on sheet #3 on another cell.
function onOpen(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var menuEntries = [];
menuEntries.push({name: "File...", functionName: "doGet"});
ss.addMenu("Attach ...", menuEntries);
}
function doGet(e) {
var app = UiApp.createApplication().setTitle("Attach file to sheet");
var form = app.createFormPanel().setId('frm').setEncoding('multipart/form-data');
var formContent = app.createVerticalPanel();
form.add(formContent);
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton('Submit'));
app.add(form);
SpreadsheetApp.getActiveSpreadsheet().show(app);
return app;
}
function doPost(e) {
var fileBlob = e.parameter.thefile;
var doc = DocsList.getFolderById('0B0uw1JCogWHuc29FWFJMWmc3Z1k').createFile(fileBlob);
var app = UiApp.getActiveApplication();
var label = app.createLabel('file uploaded successfully');
var value = '=hyperlink("' + doc.getUrl() + '","' + doc.getName() + '")'
app.add(label);
app.close();
SpreadsheetApp.getActiveSheet().getActiveCell().setValue(value);
return app;
}
I tried SpreadsheetApp.getActiveSheet().getActiveCell().setValue(value); outside of the doPost() function and this works when called in a normal context. What am I missing here?
judging from this answer, it is not possible to get the current spreadsheet/cell from the doPost function, the way I got it working is to get it in the doGet function via hidden fields and pass it via the form. Full blown working example here.

Automatic update of Button text on change

I'm currently learning scala, and making an encryption program with a basic scala swing UI.
I added 2 swing buttons which text is held by 2 var.
The code looks like this :
var encText = "Encrypt"
var decText = "Decrypt"
def top = new MainFrame {
title = "Data Guardian"
minimumSize = new Dimension(500, 200)
contents = new GridPanel(2, 2) {
hGap = 3; vGap = 3
contents += new Button {
text = encText
reactions += {
case ButtonClicked(_) => Main.startEnc
}
}
contents += new Button {
text = decText
reactions += {
case ButtonClicked(_) => Main.startDec
}
}
}
size = new Dimension(150, 40)
}
Those "text" var will be changed often during the encryption/decryption process by various methods, but when they do change, the text displayed on the buttons doesn't.
I'd like to know a way to make the displayed text of the buttons automatically change when the var that holds that text changes.
Thanks a lot for your insight :)
Make the strings private and write getters/setters that change the button text as a side-effect.
You'll need to give the buttons names, rather than having anonymous instances as you do above.