Skip to content

Commit

Permalink
InvalidSectionType Code Action #6
Browse files Browse the repository at this point in the history
  • Loading branch information
AziatkaVictor committed Apr 17, 2023
1 parent 74f648e commit 3c49661
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
63 changes: 58 additions & 5 deletions src/ltx/ltxSection.ts
Expand Up @@ -31,7 +31,7 @@ export class LtxSection {
}
}

close(line? : number) {
close(line?: number) {
if (!line) {
this.endLine = this.tempLines.size !== 0 ? Math.max(...Array.from(this.tempLines.keys())) : this.startLine;
}
Expand Down Expand Up @@ -79,7 +79,7 @@ export class LtxSection {
}

getTypeRange() {
return new Range(new Position(this.startLine, this.linkRange.start.character), new Position(this.startLine, this.linkRange.start.character + this.getTypeName().length));
return new Range(new Position(this.startLine, this.linkRange.start.character), new Position(this.startLine, this.linkRange.start.character + this.getTypeName() ? this.getTypeName().length + 1: this.name.length));
}

getParams() {
Expand All @@ -105,8 +105,8 @@ export class LtxSection {
getOwner() {
return this.owner;
}
getLinks(): LtxSectionLink[]|null {

getLinks(): LtxSectionLink[] | null {
if (!this.name) {
return;
}
Expand All @@ -126,6 +126,18 @@ export class LtxSection {
return this.getLinks() ? this.getLinks().length !== 0 : false;
}

getSimilarType(count: number, minSimilarity: number): string[] {
var data = [];
var min = minSimilarity;
for (const sectionType of getSectionData().keys()) {
var value = similarity(sectionType, this.getTypeName());
if (value >= min && data.length < count) {
data.push(sectionType);
}
}
return data.sort();
}

constructor(name: string, startLine: number, startCharacter: number, filetype: LtxDocumentType, owner: LtxDocument) {
this.owner = owner;
this.name = name.slice(1, name.length - 1).trim();
Expand All @@ -134,7 +146,48 @@ export class LtxSection {
}

this.linkRange = new Range(new Position(startLine, startCharacter + 1), new Position(startLine, startCharacter + name.length - 1));
this.startLine = startLine;
this.startLine = startLine;
addSemantic(new LtxSemantic(LtxSemanticType.struct, LtxSemanticModification.declaration, this.linkRange, LtxSemanticDescription.signal, this.name));
}
}

function similarity(s1: string, s2: string) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
var longerLength = longer.length;
if (longerLength == 0) {
return 1.0;
}
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength.toString());
}

function editDistance(s1: string, s2: string) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();

var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue),
costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return costs[s2.length];
}
24 changes: 14 additions & 10 deletions src/providers/logicCodeActionsProvider.ts
Expand Up @@ -18,14 +18,14 @@ export function provideCodeActions(document: TextDocument, range: Range | Select
continue;
}
for (const func of DiagnosticTag[error.tag]) {
result.push(func(document, range, data, error));
result = result.concat(func(document, range, data, error));
}
tags.push(error.tag);
}
return result;
}

function Action_ReplaceSectionToNil(document: TextDocument, range: Range, data: LtxDocument, error: LtxError): CodeAction {
function Action_ReplaceSectionToNil(document: TextDocument, range: Range, data: LtxDocument, error: LtxError): CodeAction[] {
const fix = new CodeAction(`Заменить на nil`, CodeActionKind.QuickFix);
fix.edit = new WorkspaceEdit();
const section = data.getSection(error.range.start);
Expand All @@ -35,20 +35,24 @@ function Action_ReplaceSectionToNil(document: TextDocument, range: Range, data:
}
fix.edit.set(document.uri, edits);
fix.edit.delete(document.uri, new Range(new Position(section.startLine, 0), new Position(section.endLine, document.lineAt(section.endLine).text.length)));
return fix;
return [fix];
}

function Action_Remove(document: TextDocument, range: Range, data: LtxDocument, error: LtxError): CodeAction {
function Action_Remove(document: TextDocument, range: Range, data: LtxDocument, error: LtxError): CodeAction[] {
const fix = new CodeAction(`Удалить секцию`, CodeActionKind.QuickFix);
fix.edit = new WorkspaceEdit();
fix.edit.delete(document.uri, new Range(error.range.start, new Position(error.range.end.line + 1, 0)));
return fix;
return [fix];
}

function Action_InvalidSectionType(document: TextDocument, range: Range, data: LtxDocument, error: LtxError) {
const sectionType = "sr_idle";
const fix = new CodeAction(`Заменить на ${sectionType}`, CodeActionKind.QuickFix);
fix.edit = new WorkspaceEdit();
fix.edit.replace(document.uri, error.range, sectionType);
return fix;
var result = [];
const section = data.getSection(error.range.start);
for (const sectionType of section.getSimilarType(3, 0.5)) {
const fix = new CodeAction(`Заменить на ${sectionType}`, CodeActionKind.QuickFix);
fix.edit = new WorkspaceEdit();
fix.edit.replace(document.uri, error.range, sectionType);
result.push(fix);
}
return result;
}

0 comments on commit 3c49661

Please sign in to comment.