Skip to content

Commit 5fdd504

Browse files
authored
Fix #42: Replace all TABS with spaces before processing (#43)
1 parent 695d9bd commit 5fdd504

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/extension.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,51 +462,84 @@ function loadAllXmlnsContent(xmlns: XmlNamespace): void {
462462
* - 'attributes': A string of pipe-separated attribute names already used in the component.
463463
*/
464464
function getComponentInformation(document: TextDocument, position: Position): Map<string, string> {
465+
// Create a new Map to store component information
465466
const componentInfo = new Map<string, string>();
467+
// Initialize empty text string
466468
let text: string = '';
469+
// Create start position at beginning of document
467470
const start: Position = new Position(0, 0);
471+
// Create range from start to current position
468472
const range: Range = new Range(start, position);
473+
// Get all text from start to current position
469474
const allText: string = document.getText(range);
475+
// Find index of last opening angle bracket
470476
let lastC: number = allText.lastIndexOf('<');
477+
// Get substring from last opening bracket
471478
text = allText.substring(lastC);
472479

473-
const blank_ = text.indexOf(' ');
474-
const break_ = text.indexOf('\n');
480+
// Find indexes of space, tab and newlines
481+
const blank_space = text.indexOf(' ');
482+
const blank_tab = text.indexOf('\t');
483+
const break_n = text.indexOf('\n');
484+
const break_r = text.indexOf('\r');
475485
let delimiter: number = -1;
486+
487+
// Get earliest space/tab position
488+
const blank_ = blank_space === -1 ? blank_tab : blank_tab === -1 ? blank_space : Math.min(blank_space, blank_tab);
489+
490+
// Get earliest newline position
491+
const break_ = break_n === -1 ? break_r : break_r === -1 ? break_n : Math.min(break_n, break_r);
492+
493+
// Determine which comes first - space/tab or newline
476494
if (blank_ < break_) {
477495
delimiter = blank_ > -1 ? blank_ : break_;
478496
} else if (break_ < blank_) {
479497
delimiter = break_ > -1 ? break_ : blank_;
480498
}
499+
// Get substring up to delimiter
481500
text = text.substring(0, delimiter);
501+
// Remove opening angle bracket
482502
text = text.replace('<', '');
503+
// If text contains colon (namespace separator)
483504
if (text.includes(':')) {
505+
// Split on colon to get prefix and component name
484506
const div = text.split(':');
485507
componentInfo.set('xmlnsPrefix', div[0]);
486508
componentInfo.set('componentName', div[1]);
487509

510+
// Initialize attributes string
488511
let attributes: string = '';
512+
// Find last occurrence of full component tag
489513
lastC = allText.lastIndexOf('<' + div[0] + ':' + div[1]);
490514
text = allText.substring(lastC);
491515

516+
// Return empty map if within attribute or tag is closed
492517
if (inAttribute(text)) {
493518
return new Map<string, string>();
494519
}
495520
if (text.includes('>')) {
496521
return new Map<string, string>();
497522
}
523+
// Initialize index for attribute parsing
498524
let index: number;
525+
// Regex to match attributes
499526
const rExp: RegExp = /(\w+=("|')([^"|']*)("|'))/g;
527+
// Find all attribute matches
500528
const rawClasses: RegExpMatchArray | null = text.match(rExp);
529+
// If attributes found, process them
501530
if (rawClasses && rawClasses.length > 0) {
502531
rawClasses?.forEach((item) => {
532+
// Get attribute name before equals sign
503533
index = item.indexOf('=');
504534
item = item.substring(0, index);
535+
// Add to pipe-separated attribute string
505536
attributes = attributes + (attributes !== '' ? '|' : '') + item;
506537
});
507538
}
539+
// Store attributes in component info
508540
componentInfo.set('attributes', attributes);
509541
}
542+
// Return the component information map
510543
return componentInfo;
511544
}
512545

0 commit comments

Comments
 (0)