I am having trouble getting my proposal provider to work properly. My objective is to provide a list of all possible imports a user can create. I can verify that the class is bound properly and is getting called on assist request from the editor. I can even see proposals being created and passed to the acceptor but nothing shows up in the editor.
So my question may be two part:
Can you see what I may be doing wrong in the following code as to explain content assist not showing in the editor
Can you explain the difference between the dispatch methods for Assignment, RuleCall, and Keyword? I find that all three methods are hit for the same content assist request, so what exactly do I code differently and what do they do differently?
Also I am using CodeMirror if that has any effect.
class mydslContentAssist extends IdeContentProposalProvider{
#Inject extension mydslGrammarAccess stAccess
#Inject mydslGlobalScopeProvider sp
#Inject extension IQualifiedNameProvider
override dispatch createProposals(Assignment assignment, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
switch(assignment) {
case stAccess.libraryModelAccess.importsAssignment_0: {
var libs = sp.descriptionData.getExportedObjectsByType(ModelPackage.eINSTANCE.libraryModel)
for (lib : libs.filter[(EObjectOrProxy as LibraryModel).name.startsWith(context.prefix)]) {
var proposal = proposalCreator.createProposal('import ' + (lib.EObjectOrProxy as LibraryModel).name + ".*;", context) [
source = lib
description = "import entire library contents"
]
acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
}
}
default : {
super._createProposals(assignment, context, acceptor)
}
}
}
override dispatch createProposals(RuleCall rulecall, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
switch(rulecall.rule) {
case importRule: {
var libs = sp.descriptionData.getExportedObjectsByType(ModelPackage.eINSTANCE.libraryModel)
for (lib : libs) {
var proposal = proposalCreator.createProposal('import ' + (lib.EObjectOrProxy as LibraryModel).name + ".*;", context) [
source = lib
description = "import entire library contents"
]
acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
}
}
default : {
super._createProposals(rulecall, context, acceptor)
}
}
}
override dispatch createProposals(Keyword keyword, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
switch (keyword) {
case stAccess.importAccess.importKeyword_0: {
var libs = sp.descriptionData.getExportedObjectsByType(ModelPackage.eINSTANCE.libraryModel)
for (lib : libs) {
var proposal = proposalCreator.createProposal('import ' + (lib.EObjectOrProxy as LibraryModel).name + ".*;", context) [
source = lib
description = "import entire library contents"
]
acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
}
}
default: {
super._createProposals(keyword, context, acceptor)
}
}
}
}
EDIT: My code is only being hit when calling content assist when typing the 'import' keyword. But not when typing the importedNamespace string
i gave it a try and could not reproduce
grammar:
Model:
imports+=Import*;
Import: 'import' importedNamespace=QualifiedNameWithWildcard ';'
;
QualifiedNameWithWildcard:
ID ("." ID)* (".*")?
;
and impl
package org.xtext.example.mydsl.web
import com.google.inject.Inject
import org.eclipse.xtext.Assignment
import org.eclipse.xtext.Keyword
import org.eclipse.xtext.RuleCall
import org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext
import org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor
import org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalProvider
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.xtext.example.mydsl.services.MyDslGrammarAccess
class MydslContentAssist extends IdeContentProposalProvider {
#Inject extension MyDslGrammarAccess stAccess
static val LIBS = #[
"XX1", "XX2", "YY1", "YY2"
]
override dispatch createProposals(Assignment assignment, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
switch(assignment) {
case stAccess.importAccess.importedNamespaceAssignment_1: {
for (lib : LIBS.filter[it.startsWith(context.prefix)]) {
var proposal = proposalCreator.createProposal(lib + ".*;", context) [
source = lib
description = "import entire library contents"
]
acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
}
}
default : {
super._createProposals(assignment, context, acceptor)
}
}
}
override dispatch createProposals(RuleCall rulecall, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
switch(rulecall.rule) {
case importRule: {
for (lib : LIBS) {
var proposal = proposalCreator.createProposal('import ' + lib + ".*;", context) [
source = lib
description = "import entire library contents"
]
acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
}
}
default : {
super._createProposals(rulecall, context, acceptor)
}
}
}
override dispatch createProposals(Keyword keyword, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
switch (keyword) {
case stAccess.importAccess.importKeyword_0: {
for (lib : LIBS) {
var proposal = proposalCreator.createProposal('import ' + lib + ".*;", context) [
source = lib
description = "import entire library contents"
]
acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
}
}
default: {
super._createProposals(keyword, context, acceptor)
}
}
}
}
Related
I am trying to overwrite a template file located in vendor/bolt/bolt/app/view/twig/editcontent/fields/_block.twig (I want to replace the "block selection" dropdown). Regarding to #1173, #1269, #5588, #3768 and #5102 this is not supported by default so I have to write a extension for this. So I tried this:
BackendBlockSelectionExtension:
namespace Bundle\Site;
use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Filesystem;
use Silex\Application;
use Bolt\Extension\SimpleExtension;
class BackendBlockSelectionExtension extends SimpleExtension
{
public function getServiceProviders()
{
return [
$this,
new BackendBlockSelectionProvider(),
];
}
}
BackendBlockSelectionProvider:
namespace Bundle\Site;
use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Filesystem;
use Silex\Application;
use Silex\ServiceProviderInterface;
class BackendBlockSelectionProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$side = $app['config']->getWhichEnd();
if ($side == 'backend') {
$path = __DIR__ . '/App/templates/Backend';
$filesystem = $app['filesystem'];
$filesystem->mountFilesystem('bolt', new Filesystem(new Local($path)));
$app['twig.loader.bolt_filesystem'] = $app->share(
$app->extend(
'twig.loader.bolt_filesystem',
function ($filesystem, $app) {
$path = __DIR__ . 'src/App/templates/Backend/';
$filesystem->prependPath($path, 'bolt');
return $filesystem;
}
)
);
}
}
public function boot(Application $app)
{
}
}
This seems to do the job, but I got an error I don't understand at all: The "bolt://app/theme_defaults" directory does not exist.
So my final question is: Does anyone have some example code how to overwrite/modify vendor/bolt/bolt/app/view/twig/editcontent/fields/_block.twig without touching the vendor folder?
This should be much simplier than this.
In your extension class overwrite protected function registerTwigPaths() function like this:
protected function registerTwigPaths()
{
if ($this->getEnd() == 'backend') {
return [
'view' => ['position' => 'prepend', 'namespace' => 'bolt']
];
}
return [];
}
private function getEnd()
{
$backendPrefix = $this->container['config']->get('general/branding/path');
$end = $this->container['config']->getWhichEnd();
switch ($end) {
case 'backend':
return 'backend';
case 'async':
// we have async request
// if the request begin with "/admin" (general/branding/path)
// it has been made on backend else somewhere else
$url = '/' . ltrim($_SERVER['REQUEST_URI'], $this->container['paths']['root']);
$adminUrl = '/' . trim($backendPrefix, '/');
if (strpos($url, $adminUrl) === 0) {
return 'backend';
}
default:
return $end;
}
}
Now you can crete a view directory in your extensions directory in which you can define templates in structure like in Bolt's default. I would start with copy and overwrite.
I am making a simple note taking app to learn node and ES6. I have 3 modules - App, NotesManager and Note. I am importing the Note class into the NotesManager and am trying to instantiate it in its addNote function. The problem is that even though the import is correct, it turns out to be undefined inside the class definition. A simpler solution would be to just instantiate the NotesManager class and add the Note class to its constructor however, I want to have NotesManager as a static utility class.
Here is my code.
Note.js
class Note {
constructor(title, body) {
this.title = title;
this.body = body;
}
}
module.exports = Note;
NotesManager.js
const note = require("./Note");
console.log("Note: ", note); //shows correctly
class NotesManager {
constructor() {}
static addNote(title, body) {
const note = new note(title, body); //Fails here as note is undefined
NotesManager.notes.push(note);
}
static getNote(title) {
if (title) {
console.log(`Getting Note: ${title}`);
} else {
console.log("Please provide a legit title");
}
}
static removeNote(title) {
if (title) {
console.log(`Removing Note: ${title}`);
} else {
console.log("Please provide a legit title");
}
}
static getAll() {
//console.log("Getting all notes ", NotesManager.notes, note);
}
}
NotesManager.notes = []; //Want notes to be a static variable
module.exports.NotesManager = NotesManager;
App.js
console.log("Starting App");
const fs = require("fs"),
_ = require("lodash"),
yargs = require("yargs"),
{ NotesManager } = require("./NotesManager");
console.log(NotesManager.getAll()); //works
const command = process.argv[2],
argv = yargs.argv;
console.log(argv);
switch (command) {
case "add":
const title = argv.title || "No title given";
const body = argv.body || "";
NotesManager.addNote(title, body); //Fails here
break;
case "list":
NotesManager.getAll();
break;
case "remove":
NotesManager.removeNote(argv.title);
break;
case "read":
NotesManager.getNote(argv.title);
break;
default:
notes.getAll();
break;
}
Is it possible for me to create a strict utility class which I can use without instantiating like in Java? Pretty new here and have tried searching for it without any luck. Thank you for your help.
When you do this:
const note = new note(title, body);
you redefine note shadowing the original note from the outer scope. You need to pick a different variable name.
Something like this should work better:
static addNote(title, body) {
const some_note = new note(title, body); //Fails here as note is undefined
NotesManager.notes.push(some_note);
}
I am using InversifyJS to setup some kind-of dynamic binding and running it on NodeJS. The problem I have is that I get errors, but without messages and only a stacktrace.
The Main class
export class Main {
public static getCalculator(config: string): Calculator {
try {
var container: Container = new KeyFeatureContainer(config).getContainer();
debugger;
// error here !
return container.get<Calculator>(TYPES.Calculator);
} catch (error) {
debugger;
return null;
}
}
}
This class calls the KeyFeatureContainer with a json string, used to configure all the bindings (in a sort-of dynamic way), and then retrieves the container.
The KeyFeatureContainer class
export class KeyFeatureContainer {
private _container: Container;
public constructor(config: string) {
var jsonConfig: any[] = JSON.parse(config);
this._container = new Container();
this._container.bind<Calculator>(TYPES.Calculator).to(KeyFeatureCalculator);
for (var i = 0; i < jsonConfig.length; i++) {
if (jsonConfig[i].active) {
this.parseConfigKeyFeatures(jsonConfig[i].id);
this.parseConfigParams(jsonConfig[i].params);
}
}
}
public getContainer(): Container {
debugger;
return this._container;
}
private parseConfigKeyFeatures(id: string): void {
var keyFeatureContainerModule: ContainerModule = rootContainer.get<KeyFeatureContainerModule>(id).getContainerModule();
if (keyFeatureContainerModule != null)
this._container.load(keyFeatureContainerModule);
}
private parseConfigParams(params: Array<{ name: string, value: any }>): void {
for (var param of params)
this._container.bind(param.name).to(param.value);
}
}
This class receives the json configuration and loads, following an ID, a ContainerModule which contains the binding between the interface and the concrete implementation. The rootContainer specifies the ContainerModule to retrieve following an ID.
The rootContainer
var rootContainer: Container = new Container();
rootContainer.bind<KeyFeatureContainerModule>(KEYFEATURES.DrugHoliday).to(DrugHolidayContainerModule);
rootContainer.bind<KeyFeatureContainerModule>(KEYFEATURES.MissingDay).to(MissingDayContainerModule);
export default rootContainer;
And the related ContainerModules (the second one is identical, just the PARAMS that are different for each)
#injectable()
export class MissingDayKeyFeature implements KeyFeature {
#inject(PARAMS.MissingDayParams.NbIntakesLimit)
private _nbIntakesLimit: number;
#inject(PARAMS.MissingDayParams.ExtraParamA)
private _extraParamA: any;
#inject(PARAMS.MissingDayParams.ExtraParamB)
private _extraParamB: any;
public init(): void {
console.log("init() at MissingDay");
console.log("nbIntakesLimit = " + this._nbIntakesLimit);
console.log("extraParamA = " + this._extraParamA);
console.log("extraParamB = " + this._extraParamB);
}
public calculate(): void {
console.log("calculate() at MissingDay");
}
public finish(): void {
console.log("finish() at MissingDay");
}
}
#injectable()
export class MissingDayContainerModule implements KeyFeatureContainerModule {
public getContainerModule(): ContainerModule {
return new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind) => {
bind<KeyFeature>(TYPES.KeyFeature).to(MissingDayKeyFeature);
});
}
}
And finally, the ServiceIdentifiers used to setup the whole (separated files)
let TYPES = {
KeyFeature: "KeyFeature",
Calculator: "Calculator"
}
export default TYPES;
let PARAMS = {
DrugHolidayParams: {
NbDaysLimit: "nbDaysLimit",
ExtraParamA: "extraParamDHA",
ExtraParamB: "extraParamDHB"
},
MissingDayParams: {
NbIntakesLimit: "nbIntakesLimit",
ExtraParamA: "extraParamMDA",
ExtraParamB: "extraParamMDB"
}
}
export default PARAMS;
let KEYFEATURES = {
MissingDay: "MissingDayKeyFeature",
DrugHoliday: "DrugHolidayKeyFeature"
}
export default KEYFEATURES;
Finally the JSON input (formatted for convenience)
[{
"id": "DrugHolidayKeyFeature",
"active": true,
"params": [{
"name": "nbDaysLimit",
"value": 3
}, {
"name": "extraParamDHA",
"value": "DHA"
}, {
"name": "extraParamDHB",
"value": "DHB"
}
]
}, {
"id": "MissingDayKeyFeature",
"active": false,
"params": [{
"name": "nbIntakesLimit",
"value": 0
}, {
"name": "extraParamMDA",
"value": "MDA"
}, {
"name": "extraParamMDB",
"value": "MDB"
}
]
}
]
I use a simple Test script just to output the values and see if all bindings are done correctly
import { Main } from "./Main";
var json: string = *json above*;
Main.getCalculator(json).calculate();
But I end up with this error
Error
< at _createSubRequests (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\planning\planner.js:106:19)
< at Object.plan (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\planning\planner.js:125:5)
< at D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\container\container.js:205:37
< at Container._get (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\container\container.js:198:44)
< at Container.get (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\container\container.js:163:21)
< at Function.getCalculator (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\transpiled\KFCalc\Main.js:9:30)
< at Object.<anonymous> (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\transpiled\KFCalc\Test.js:4:13)
< at Module._compile (module.js:573:32)
< at Object.Module._extensions..js (module.js:582:10)
< at Module.load (module.js:490:32)
There is no clue about what is happening, and I can't figure out what I have done wrong. I can't get<Calculator> nor get<KeyFeature>, but if I do :
container.isBound(TYPES.Calculator) // returns true !!!
and same applies for all bound items.
Thank you for any pointers, I'm out of ideas.
+-- #types/node#7.0.2
+-- inversify#3.0.0
+-- reflect-metadata#0.1.9
+-- typescript#2.0.10
EDIT : Forgot the KeyFeatureCalculator
#injectable()
export class KeyFeatureCalculator implements Calculator {
// multi-injection of bound key features
private _keyFeatureCalculators: KeyFeature[] = [];
public constructor(#multiInject(TYPES.KeyFeature) keyFeatureCalculators: KeyFeature[]) {
this._keyFeatureCalculators = keyFeatureCalculators;
}
public calculate(): void {
console.log("calculate() at KeyFeatureCalculator");
for (var calculator of this._keyFeatureCalculators) {
calculator.init();
calculator.calculate();
calculator.finish();
}
}
}
Also, all my files (or almost) import inject, injectable from inversify and also import reflect-metadata
Error resolved : For those who didn't pay attention, each KeyFeature object has its properties injected, and their types are number or any. Since the values are retrieved from my JSON, they are typed as any and thus the compiler doesn't warn me that the binding bind(xxx).to(yyy) requires to be a newable element !
So, in my example, the yyy was something like 3, which is a number and thus not boundable. I changed to bind(xxx).toConstantValue(yyy) and everything works !
I'm trying to get dart to play an audio, but everytime I try to play the Audio I get the following error:
Internal error: 'dart:_blink': error: line 248: native function 'AudioBufferSourceNode_noteOn_Callback_RESOLVER_STRING_1_double' (2 arguments) cannot be found
Native_AudioBufferSourceNode_noteOn_Callback(mthis, when) native "AudioBufferSourceNode_noteOn_Callback_RESOLVER_STRING_1_double";
This is the my Audio class:
class Audio {
static List<Audio> _loadQueue = new List<Audio>();
String _url;
bool loaded = false;
AudioBufferSourceNode _source;
Audio(this._url) {
if (audioContext == null) {
_loadQueue.add(this);
}
else {
load();
}
}
void load() {
print("Loading sound: " + _url);
HttpRequest req = new HttpRequest();
req.open("GET", _url);
req.responseType = "arraybuffer";
req.onLoad.listen((e) {
_source = audioContext.createBufferSource();
print("Found sound: " + _url + ". Decoding...");
audioContext.decodeAudioData(req.response).then((buffer) {
_source.buffer = buffer;
_source.connectNode(gainNode);
_source.connectNode(audioContext.destination);
loaded = true;
});
});
req.send();
}
void play() {
if (!loaded) { return; }
_source.noteOn(0);
}
void stop() {
if (!loaded) return;
_source.noteOff(0);
}
static void loadAll() {
_loadQueue.forEach((e) {e.load();});
}
}
The audio context and gain node is created in another class like this:
audioContext = new AudioContext();
gainNode = audioContext.createGain();
gainNode.connectNode(audioContext.destination);
Audio.loadAll();
I don't know what the problem is, especially since it says it's internal, and that it's missing arguments, but the noteOn function only takes one argument.
I was facing the same issue using dart 1.6. It seems noteOn has been replaced by start in the latest spec/implementation.
The AudioBufferSourceNode Interface (http://webaudio.github.io/web-audio-api/#the-audiobuffersourcenode-interface)
Porting notes (https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Porting_webkitAudioContext_code_to_standards_based_AudioContext#Changes_to_methods_used_to_start.2Fstop_AudioBufferSourceNode_and_OscillatorNode)
In Objective-C, I know how passing a protocol as parameter:
- (void)MyMethod:(Protocol *)myparameter
But in Swift there is no more Protocol type.
How can I pass a protocol as parameter without knowing which is ?
In one of your comments you say:
"I want create a method which return an array of type of class which implements a desired protocol."
Have you tried something like the following:
//notice the use of #objc here
#objc protocol AlertProtocol
{
func getMyName()->String
}
class Class1 : AlertProtocol
{
let name = "Object 1"
func getMyName() -> String
{
return name
}
}
class Class2 : AlertProtocol
{
let name = "Object 2"
func getMyName() -> String
{
return name
}
}
//borrowing from and refactoring siLo's answer
func classesConformingToProtocol(proto:Protocol) -> [AnyClass]
{
let availableClasses : [AnyClass] = [ Class1.self, Class2.self ]
var conformingClasses = Array<AnyClass>()
for myClass : AnyClass in availableClasses
{
if myClass.conforms(to: proto)
{
conformingClasses.append(myClass)
}
}
return conformingClasses
}
Then use the above structure like this:
let classes = classesConformingToProtocol(AlertProtocol.self)
The tricky part that does the work is the "#objc" that exposes the protocol to the objective c runtime and allows us to pass any "Protocol Type" as a parameter.
Probably at some point in the future we will be able to do this in a "pure" Swift way.
Here is what I have tried:
#objc protocol Walker
{
func walk()
}
#objc protocol Runner
{
func run()
}
#objc class Zombie : Walker
{
func walk () { println("Brains...") }
}
#objc class Survivor : Runner
{
func run() { println("Aaaah, zombies!") }
}
func classesConformingToProtocol(proto:Protocol) -> AnyClass[]
{
let availableClasses : AnyClass[] = [ Zombie.self, Survivor.self ]
var conformingClasses = Array<AnyClass>()
for myClass : AnyClass in availableClasses
{
if myClass.conformsToProtocol(proto)
{
conformingClasses.append(myClass)
}
}
return conformingClasses
}
// This does not work
let walkers = classesConformingToProtocol(Walker.self)
let runners = classesConformingToProtocol(Runner.self)
I have been unable to convert Swift's Metatype information into a Protocol object.
In swift 2.0, I use it like this before:
classA.conformsToProtocol(XXXProtocol.self as! Protocol)
It doesn't works fine...
Look the definition of Protocol:
// All methods of class Protocol are unavailable.
// Use the functions in objc/runtime.h instead.
#available(iOS 2.0, *)
public class Protocol {
}
All are unavailable...and I don't know which to use instead in objc/runtime.h
So I have to use this method:
if ClassA is protocol<XXXProtocol> {
// do something
}
Currently, it works...
If you don't allow use #objc (because yours protocols have property, for example), the only solution that I found is with closure. Then, you need use a closure to use a protocol and return a value.
protocol Proto { }
protocol Proto2 { }
class Foo: Proto { }
class Bar: Proto, Proto2 { }
class Baz: Proto2 { }
class Qux { }
func printConforms(classList: [AnyClass], protoCond: (AnyClass) -> Any?) {
for i in classList {
print(i, terminator: " -> ")
if protoCond(i) != nil {
print("is subscriber")
} else {
print("NOT IS subscriber")
}
}
}
let myClasses: [AnyClass] = [Foo.self, Bar.self, Baz.self, Qux.self]
printConforms(classList: myClasses, protoCond: { $0 as? Proto.Type })
More complete example: https://gist.github.com/brunomacabeusbr/eea343bb9119b96eed3393e41dcda0c9
Edit
Another better solution is using generics, for example:
protocol Proto { }
class Foo: Proto { }
class Bar: Proto { }
class Baz { }
func filter<T>(classes: [AnyClass], byConformanceTo: T.Type) -> [AnyClass] {
return classes.filter { $0 is T }
}
filter(classes: [Foo.self, Bar.self, Baz.self], byConformanceTo: Proto.Type.self)
// return [Foo.self, Bar.self]
Worked out a way today (Xcode 6.1):
Firstly, the protocol must be marked as #objc for any checking to work.
Then use an "if let" cast to check for conformance.
#objc protocol MyProtocol {
var protocolValue: Int { get set }
}
if let conformingObject = someObject as? MyProtocol {
// conformingObject is now someObject cast to MyProtocol
conformingObject.protocolValue = 3
}