Skip to content

Commit

Permalink
Added support for Apex (#2622)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Nov 20, 2020
1 parent a5107d5 commit f0e2b70
Show file tree
Hide file tree
Showing 17 changed files with 794 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
"title": "Apache Configuration",
"owner": "GuiTeK"
},
"apex": {
"title": "Apex",
"require": ["clike", "sql"],
"owner": "RunDevelopment"
},
"apl": {
"title": "APL",
"owner": "ngn"
Expand Down
65 changes: 65 additions & 0 deletions components/prism-apex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(function (Prism) {

var keywords = /\b(?:abstract|activate|and|any|array|as|asc|autonomous|begin|bigdecimal|blob|boolean|break|bulk|by|byte|case|cast|catch|char|class|collect|commit|const|continue|currency|date|datetime|decimal|default|delete|desc|do|double|else|end|enum|exception|exit|export|extends|final|finally|float|for|from|global|goto|group|having|hint|if|implements|import|in|inner|insert|instanceof|int|integer|interface|into|join|like|limit|list|long|loop|map|merge|new|not|null|nulls|number|object|of|on|or|outer|override|package|parallel|pragma|private|protected|public|retrieve|return|rollback|select|set|short|sObject|sort|static|string|super|switch|synchronized|system|testmethod|then|this|throw|time|transaction|transient|trigger|try|undelete|update|upsert|using|virtual|void|webservice|when|where|while|get(?=\s*[{};])|(?:after|before)(?=\s+[a-z])|(?:inherited|with|without)\s+sharing)\b/i;

var className = /\b(?:(?=[a-z_]\w*\s*[<\[])|(?!<keyword>))[A-Z_]\w*(?:\s*\.\s*[A-Z_]\w*)*\b(?:\s*(?:\[\s*\]|<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>))*/.source
.replace(/<keyword>/g, function () { return keywords.source; });
/** @param {string} pattern */
function insertClassName(pattern) {
return RegExp(pattern.replace(/<CLASS-NAME>/g, function () { return className; }), 'i');
}

var classNameInside = {
'keyword': keywords,
'punctuation': /[()\[\]{};,:.<>]/
};

Prism.languages.apex = {
'comment': Prism.languages.clike.comment,
'string': Prism.languages.clike.string,
'sql': {
pattern: /((?:[=,({:]|\breturn)\s*)\[[^\[\]]*\]/i,
lookbehind: true,
greedy: true,
alias: 'language-sql',
inside: Prism.languages.sql
},

'annotation': {
pattern: /@\w+\b/,
alias: 'punctuation'
},
'class-name': [
{
pattern: insertClassName(/(\b(?:class|enum|extends|implements|instanceof|interface|new|trigger\s+\w+\s+on)\s+)<CLASS-NAME>/.source),
lookbehind: true,
inside: classNameInside
},
{
// cast
pattern: insertClassName(/(\(\s*)<CLASS-NAME>(?=\s*\)\s*[\w(])/.source),
lookbehind: true,
inside: classNameInside
},
{
// variable/parameter declaration and return types
pattern: insertClassName(/<CLASS-NAME>(?=\s*\w+\s*[;=,(){:])/.source),
inside: classNameInside
}
],
'trigger': {
pattern: /(\btrigger\s+)\w+\b/i,
lookbehind: true,
alias: 'class-name'
},
'keyword': keywords,
'function': /\b[a-z_]\w*(?=\s*\()/i,

'boolean': /\b(?:false|true)\b/i,

'number': /(?:\B\.\d+|\b\d+(?:\.\d+|L)?)\b/i,
'operator': /[!=](?:==?)?|\?\.?|&&|\|\||--|\+\+|[-+*/^&|]=?|:|<<?=?|>{1,3}=?/,
'punctuation': /[()\[\]{};,.]/
};

})(Prism);
1 change: 1 addition & 0 deletions components/prism-apex.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 152 additions & 0 deletions examples/prism-apex.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<h2>Full example</h2>
<pre><code>// source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_shopping_cart_example_code.htm

trigger calculate on Item__c (after insert, after update, after delete) {

// Use a map because it doesn't allow duplicate values

Map&lt;ID, Shipping_Invoice__C> updateMap = new Map&lt;ID, Shipping_Invoice__C>();

// Set this integer to -1 if we are deleting
Integer subtract ;

// Populate the list of items based on trigger type
List&lt;Item__c> itemList;
if(trigger.isInsert || trigger.isUpdate){
itemList = Trigger.new;
subtract = 1;
}
else if(trigger.isDelete)
{
// Note -- there is no trigger.new in delete
itemList = trigger.old;
subtract = -1;
}

// Access all the information we need in a single query
// rather than querying when we need it.
// This is a best practice for bulkifying requests

set&lt;Id> AllItems = new set&lt;id>();

for(item__c i :itemList){
// Assert numbers are not negative.
// None of the fields would make sense with a negative value

System.assert(i.quantity__c > 0, 'Quantity must be positive');
System.assert(i.weight__c >= 0, 'Weight must be non-negative');
System.assert(i.price__c >= 0, 'Price must be non-negative');

// If there is a duplicate Id, it won't get added to a set
AllItems.add(i.Shipping_Invoice__C);
}

// Accessing all shipping invoices associated with the items in the trigger
List&lt;Shipping_Invoice__C> AllShippingInvoices = [SELECT Id, ShippingDiscount__c,
SubTotal__c, TotalWeight__c, Tax__c, GrandTotal__c
FROM Shipping_Invoice__C WHERE Id IN :AllItems];

// Take the list we just populated and put it into a Map.
// This will make it easier to look up a shipping invoice
// because you must iterate a list, but you can use lookup for a map,
Map&lt;ID, Shipping_Invoice__C> SIMap = new Map&lt;ID, Shipping_Invoice__C>();

for(Shipping_Invoice__C sc : AllShippingInvoices)
{
SIMap.put(sc.id, sc);
}

// Process the list of items
if(Trigger.isUpdate)
{
// Treat updates like a removal of the old item and addition of the
// revised item rather than figuring out the differences of each field
// and acting accordingly.
// Note updates have both trigger.new and trigger.old
for(Integer x = 0; x &lt; Trigger.old.size(); x++)
{
Shipping_Invoice__C myOrder;
myOrder = SIMap.get(trigger.old[x].Shipping_Invoice__C);

// Decrement the previous value from the subtotal and weight.
myOrder.SubTotal__c -= (trigger.old[x].price__c *
trigger.old[x].quantity__c);
myOrder.TotalWeight__c -= (trigger.old[x].weight__c *
trigger.old[x].quantity__c);

// Increment the new subtotal and weight.
myOrder.SubTotal__c += (trigger.new[x].price__c *
trigger.new[x].quantity__c);
myOrder.TotalWeight__c += (trigger.new[x].weight__c *
trigger.new[x].quantity__c);
}

for(Shipping_Invoice__C myOrder : AllShippingInvoices)
{

// Set tax rate to 9.25% Please note, this is a simple example.
// Generally, you would never hard code values.
// Leveraging Custom Settings for tax rates is a best practice.
// See Custom Settings in the Apex Developer Guide
// for more information.
myOrder.Tax__c = myOrder.Subtotal__c * .0925;

// Reset the shipping discount
myOrder.ShippingDiscount__c = 0;

// Set shipping rate to 75 cents per pound.
// Generally, you would never hard code values.
// Leveraging Custom Settings for the shipping rate is a best practice.
// See Custom Settings in the Apex Developer Guide
// for more information.
myOrder.Shipping__c = (myOrder.totalWeight__c * .75);
myOrder.GrandTotal__c = myOrder.SubTotal__c + myOrder.tax__c +
myOrder.Shipping__c;
updateMap.put(myOrder.id, myOrder);
}
}
else
{
for(Item__c itemToProcess : itemList)
{
Shipping_Invoice__C myOrder;

// Look up the correct shipping invoice from the ones we got earlier
myOrder = SIMap.get(itemToProcess.Shipping_Invoice__C);
myOrder.SubTotal__c += (itemToProcess.price__c *
itemToProcess.quantity__c * subtract);
myOrder.TotalWeight__c += (itemToProcess.weight__c *
itemToProcess.quantity__c * subtract);
}

for(Shipping_Invoice__C myOrder : AllShippingInvoices)
{

// Set tax rate to 9.25% Please note, this is a simple example.
// Generally, you would never hard code values.
// Leveraging Custom Settings for tax rates is a best practice.
// See Custom Settings in the Apex Developer Guide
// for more information.
myOrder.Tax__c = myOrder.Subtotal__c * .0925;

// Reset shipping discount
myOrder.ShippingDiscount__c = 0;

// Set shipping rate to 75 cents per pound.
// Generally, you would never hard code values.
// Leveraging Custom Settings for the shipping rate is a best practice.
// See Custom Settings in the Apex Developer Guide
// for more information.
myOrder.Shipping__c = (myOrder.totalWeight__c * .75);
myOrder.GrandTotal__c = myOrder.SubTotal__c + myOrder.tax__c +
myOrder.Shipping__c;

updateMap.put(myOrder.id, myOrder);

}
}

// Only use one DML update at the end.
// This minimizes the number of DML requests generated from this trigger.
update updateMap.values();
}</code></pre>
4 changes: 4 additions & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
var lang_dependencies = /*dependencies_placeholder[*/{
"javascript": "clike",
"actionscript": "javascript",
"apex": [
"clike",
"sql"
],
"arduino": "cpp",
"aspnet": [
"markup",
Expand Down
Loading

0 comments on commit f0e2b70

Please sign in to comment.