Skip to content

Commit

Permalink
Implement local labels prefix (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Settis authored Jan 14, 2024
1 parent be7b004 commit d2b418b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Changelog
## [Unreleased]
### Braking change

- deleted configuration for completely hiding labels from autocompletion

### Added

- Added configuration for the current file only label autocompletion

## [1.4.1] - 2023-12-16

Expand Down
4 changes: 2 additions & 2 deletions e2eTests/staticCases/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"dasm.labels.hidePrefix" : ["_TEST_"]
}
"dasm.labels.localPrefix": ["_TEST_"]
}
1 change: 0 additions & 1 deletion e2eTests/useCases/labelsCompletion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ actions:
noHide:
type: Completion
text: _TEST_HIDING
not: True
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
"engines": {
"vscode": "^1.52.0"
},
"activationEvents": [
"onLanguage:dasm"
],
"main": "./out/client",
"contributes": {
"languages": [
Expand All @@ -45,20 +42,20 @@
"configuration": {
"title": "DASM",
"properties": {
"dasm.labels.hidePrefix": {
"dasm.labels.localPrefix": {
"type": "array",
"default": [],
"items": {
"type": "string"
},
"uniqueItems": true,
"description": "Hide labels in autocompletion by this prefix"
"description": "Hide labels in autocompletion in other files by this prefix"
}
}
},
"configurationDefaults": {
"[dasm]": {
"editor.wordBasedSuggestions": false
"editor.wordBasedSuggestions": "off"
}
}
},
Expand Down
31 changes: 25 additions & 6 deletions server/src/completion/label.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { CompletionItem, CompletionItemKind } from "vscode-languageserver";
import { Program } from "../program";
import { getDocumentSettings } from "../server";
import { LabelObject } from "../parser/ast/labels";
import { AstNode } from "../parser/ast/nodes";

export async function onLabelCompletion(program: Program | undefined, includeConstant = false): Promise<CompletionItem[]> {
if (program === undefined) return [];
const result: CompletionItem[] = [];
const hidePrefixes = (await getDocumentSettings(program.uri)).labels.hidePrefix ?? [];
const localPrefixes = (await getDocumentSettings(program.uri)).labels.localPrefix ?? [];
const localLabelsChecker = new LocalLabelsChecker(program.uri, localPrefixes);
for (const label of program.globalLabels.values()) {
if ((includeConstant || label.definedAsVariable) && canShow(label.name, hidePrefixes)) {
if ((includeConstant || label.definedAsVariable) && localLabelsChecker.canShow(label)) {
result.push({
label: label.name,
kind: label.definedAsVariable ? CompletionItemKind.Variable : CompletionItemKind.Constant
Expand All @@ -17,8 +20,24 @@ export async function onLabelCompletion(program: Program | undefined, includeCon
return result;
}

function canShow(labenName: string, hidePrefixes: string[]): boolean {
for (const prefix of hidePrefixes)
if (labenName.startsWith(prefix)) return false;
return true;
class LocalLabelsChecker {
public constructor(private uri: string, private localPrefixes: string[]) {}

public canShow(label: LabelObject): boolean {
if (this.hasLocalPrefix(label.name))
return this.theSameDocument(label.definitions);
return true;
}

private hasLocalPrefix(labelName: string): boolean {
for (const prefix of this.localPrefixes)
if (labelName.startsWith(prefix)) return true;
return false;
}

private theSameDocument(definitions: AstNode[]): boolean {
for (const definition of definitions)
if (definition.location.uri === this.uri) return true;
return false;
}
}
4 changes: 2 additions & 2 deletions server/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ export type Settings = {
}

export type LabelsSettings = {
hidePrefix: string[] | null | undefined
localPrefix: string[] | null | undefined
}

export const DEFAULT_SETTINGS: Settings = {
labels: {
hidePrefix: []
localPrefix: []
}
};

0 comments on commit d2b418b

Please sign in to comment.