Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 116 additions & 14 deletions server/src/capabilities/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ export enum ScopeType {
VARIABLE,
/** A variable declaration in a signature */
PARAMETER,
/** Any reference type that isn't a declaration. */
REFERENCE
/** A reference to some type of declaration. */
REFERENCE,
/** A special reference type. */
ATTRIBUTE
}

export enum AssignmentType {
Expand All @@ -211,6 +213,7 @@ export class ScopeItemCapability {
};
parameters?: Map<string, ScopeItemCapability[]>;
references?: Map<string, ScopeItemCapability[]>;
attributes?: Map<string, ScopeItemCapability[]>;

// Special scope references for easier resolution of names.
implicitDeclarations?: Map<string, ScopeItemCapability[]>;
Expand Down Expand Up @@ -288,8 +291,10 @@ export class ScopeItemCapability {
* Recursively build from this node down.
*/
build(): void {
if (this.type === ScopeType.REFERENCE) {
// Link to declaration if it exists.
if (this.type === ScopeType.ATTRIBUTE) {
this.resolveAttribute();
this.validateAttributes();
} else if (this.type === ScopeType.REFERENCE) {
this.resolveLinks();
this.validateLink();
} else {
Expand All @@ -308,6 +313,7 @@ export class ScopeItemCapability {
this.properties?.letters?.forEach(items => items.forEach(item => item.build()));
this.properties?.setters?.forEach(items => items.forEach(item => item.build()));
this.references?.forEach(items => items.forEach(item => item.build()));
this.attributes?.forEach(items => items.forEach(item => item.build()));

this.isDirty = false;
}
Expand Down Expand Up @@ -460,6 +466,88 @@ export class ScopeItemCapability {
}
}

private resolveAttribute() {
/**
* Most attributes will be belong to the parent. Variable attributes
* will belong to the same scope as the item they refer to.
*
* We set one way links here to facilitate renaming.
* Setting an attribute as a back link will impact unused diagnostics.
*/

if (!this.parent) {
Services.logger.error(`Expected parent for attribute ${this.name}`);
throw new Error("Attribute scope item has no parent.");
}

// The immediate parent is probably the linked item.
if (this.name === this.parent.name) {
this.link = this.parent;
return;
}

// If not, we may be a variable attribute (shared parent).
const declarations = this.parent.properties?.getters?.get(this.name);
if (!declarations) {
return;
}

// Handle a single declaration found.
if (declarations.length === 1) {
this.link = declarations[0];
this.parent.moveAttribute(this, declarations[0]);
return;
}

// Handle duplicate declarations by attaching to the closest above.
const targetRow = this.range?.start.line ?? 0;
let closestDeclaration: ScopeItemCapability | undefined;
for (const declaration of declarations) {
const declarationRow = declaration?.range?.start.line ?? 0;
if (declarationRow === 0 || declarationRow >= targetRow) {
return;
}

if (!closestDeclaration) {
closestDeclaration = declaration;
return;
}

const closestRow = closestDeclaration.range?.start.line ?? 0;
if (targetRow > declarationRow && declarationRow > closestRow) {
closestDeclaration = declaration;
}
}

if (closestDeclaration) {
this.link = closestDeclaration;
this.parent.moveAttribute(this, closestDeclaration);
}
}

moveAttribute(attr: ScopeItemCapability, destination: ScopeItemCapability) {
const items = this.attributes?.get(attr.name);
if (!items || items.length === 0) {
return;
}

const unmoved: ScopeItemCapability[] = [];
items.forEach(item => {
const isLocMatch = item.locationUri === attr.locationUri;
const isRangeMatch = rangeEquals(item.element?.context.range, attr.range);
if (isLocMatch && isRangeMatch) {
destination.attributes ??= new Map();
destination.addItem(destination.attributes, item);
} else {
unmoved.push(item);
}
});
}

private validateAttributes() {
// Attributes must be in specific locations to work.
}

private resolveLinks() {

// Resolve where we have no member access names.
Expand Down Expand Up @@ -726,6 +814,13 @@ export class ScopeItemCapability {
return this;
}

// Register attributes
if (item.type === ScopeType.ATTRIBUTE) {
item.parent.attributes ??= new Map();
item.parent.addItem(item.parent.attributes, item);
return this;
}

// Add implicitly accessible names to the project scope.
if (item.isPublicScope && this.project && this !== this.project) {
this.project.implicitDeclarations ??= new Map();
Expand Down Expand Up @@ -913,10 +1008,16 @@ export class ScopeItemCapability {

private getItemsIdentifiedAtPosition(position: Position, results: ScopeItemCapability[] = [], searchItems: ScopeItemCapability[] = []): void {
while (searchItems.length > 0) {
// Get the next scope to search.
const scope = searchItems.pop();
if (scope === undefined) continue;

// Get the standard maps and add attributes to them if they exist.
const scopeMaps = scope.maps ?? [];
if (scope.attributes) scopeMaps.push(scope.attributes);

// Check all items for whether they have a name overlap or a scope overlap.
scope?.maps.forEach(map => map.forEach(items => items.forEach(item => {
scopeMaps.forEach(map => map.forEach(items => items.forEach(item => {
const elementRange = item.range;
const identifierRange = item.element?.identifierCapability?.range;
if (identifierRange && isPositionInsideRange(position, identifierRange)) {
Expand All @@ -931,13 +1032,13 @@ export class ScopeItemCapability {
}

getRenameItems(uri: string, position: Position): ScopeItemCapability[] {
const module = this.findModuleByUri(uri);
if (!module) {
const moduleScope = this.findModuleByUri(uri);
if (!moduleScope) {
return [];
}

const itemsAtPosition: ScopeItemCapability[] = [];
this.getItemsIdentifiedAtPosition(position, itemsAtPosition, [module]);
this.getItemsIdentifiedAtPosition(position, itemsAtPosition, [moduleScope]);
if (itemsAtPosition.length === 0) {
Services.logger.warn(`Nothing to rename.`);
return [];
Expand All @@ -956,14 +1057,15 @@ export class ScopeItemCapability {
item.parent.properties.letters?.get(item.identifier)
]
: item
).flat().flat().flat().filter(x => !!x);
).flat(2).filter(x => !!x);

// Add backlinks for each item.
const addedBacklinks = propertyIncludedItems.map(item =>
item.backlinks ? [item, ...item.backlinks] : item
).flat().flat();
// Add backlinks and attributes for each item.
const addedReferences = propertyIncludedItems.map(item => [
item.backlinks ? [item, ...item.backlinks] : item,
item.attributes?.get(item.name) ? [item, ...item.attributes.get(item.name)!] : item
]).flat(2);

const uniqueItemsAtPosition = this.removeDuplicatesByRange(addedBacklinks);
const uniqueItemsAtPosition = this.removeDuplicatesByRange(addedReferences);
return uniqueItemsAtPosition;
}

Expand Down
22 changes: 22 additions & 0 deletions server/src/project/elements/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Core
import { TextDocument } from "vscode-languageserver-textdocument";

// Antlr
import { AttributeStatementContext } from "../../antlr/out/vbaParser";

// Project
import { BaseRuleSyntaxElement } from "./base";
import { IdentifierCapability, ScopeItemCapability, ScopeType } from "../../capabilities/capabilities";


export class AttributeElement extends BaseRuleSyntaxElement<AttributeStatementContext> {
identifierCapability: IdentifierCapability;
scopeItemCapability: ScopeItemCapability;

constructor(ctx: AttributeStatementContext, doc: TextDocument) {
super(ctx, doc);

this.identifierCapability = new IdentifierCapability(this, () => ctx.ambiguousIdentifier());
this.scopeItemCapability = new ScopeItemCapability(this, ScopeType.ATTRIBUTE);
}
}
11 changes: 9 additions & 2 deletions server/src/project/parser/vbaListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
AmbiguousIdentifierContext,
AnyOperatorContext,
ArgumentListContext,
AttributeStatementContext,
CallStatementContext,
ClassModuleContext,
DictionaryAccessExpressionContext,
Expand Down Expand Up @@ -44,7 +45,7 @@ import {
WithStatementContext
} from '../../antlr/out/vbaParser';
import {
AttributeStatementContext,
AttributeStatementContext as FmtAttributeStatementContext,
BasicStatementContext,
BlockContext,
CaseDefaultStatementContext,
Expand Down Expand Up @@ -88,6 +89,7 @@ import {
PropertyLetDeclarationElement,
PropertySetDeclarationElement,
} from '../elements/procedure';
import { AttributeElement } from '../elements/attributes';


enum ParserAssignmentState {
Expand Down Expand Up @@ -181,6 +183,11 @@ export class VbaListener extends vbaListener {
this.document.registerElement(element);
};

enterAttributeStatement = (ctx: AttributeStatementContext) => {
const element = new AttributeElement(ctx, this.document.textDocument);
this.document.registerElement(element);
};

enterEnumDeclaration = (ctx: EnumDeclarationContext) => {
const element = new EnumDeclarationElement(ctx, this.document.textDocument, this.isAfterMethodDeclaration);
this.document.registerElement(element);
Expand Down Expand Up @@ -613,7 +620,7 @@ export class VbaFmtListener extends vbafmtListener {
}

// Attributes are always zero indented.
enterAttributeStatement = (ctx: AttributeStatementContext) => {
enterAttributeStatement = (ctx: FmtAttributeStatementContext) => {
const range = this.getCtxRange(ctx);
const offset = ctx.endsWithLineEnding ? 0 : 1;

Expand Down