Skip to content

Commit

Permalink
Fixes to language server
Browse files Browse the repository at this point in the history
Signed-off-by: worksofliam <[email protected]>
  • Loading branch information
worksofliam committed Dec 11, 2024
1 parent 4729f34 commit 71b6491
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 54 deletions.
1 change: 1 addition & 0 deletions extension/server/src/providers/completionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default async function completionItemProvider(handler: CompletionParams):

// If they're typing inside of a procedure, let's get the stuff from there too
const currentProcedure = doc.procedures.find((proc, index) =>
proc.range.start && proc.range.end &&
lineNumber >= proc.range.start &&
(lineNumber <= proc.range.end+1 || index === doc.procedures.length-1) &&
currentPath === proc.position.path
Expand Down
8 changes: 4 additions & 4 deletions extension/server/src/providers/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export default async function definitionProvider(handler: DefinitionParams): Pro
return Location.create(
def.position.path,
Range.create(
def.position.line,
def.position.range.line,
0,
def.position.line,
def.position.range.line,
0
)
);
Expand All @@ -49,9 +49,9 @@ export default async function definitionProvider(handler: DefinitionParams): Pro
return Location.create(
def.position.path,
Range.create(
def.position.line,
def.position.range.line,
0,
def.position.line,
def.position.range.line,
0
)
);
Expand Down
51 changes: 26 additions & 25 deletions extension/server/src/providers/documentSymbols.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DocumentSymbol, DocumentSymbolParams, Range, SymbolKind } from 'vscode-languageserver';
import { documents, parser, prettyKeywords } from '.';
import Cache from '../../../../language/models/cache';
import { Position } from '../../../../language/models/DataPoints';

export default async function documentSymbolProvider(handler: DocumentSymbolParams): Promise<DocumentSymbol[]> {
const currentPath = handler.textDocument.uri;
Expand All @@ -18,14 +19,14 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
const currentScopeDefs: DocumentSymbol[] = [];

scope.procedures
.filter(proc => proc.position && proc.position.path === currentPath)
.filter(proc => proc.position && proc.position.path === currentPath && proc.range.start && proc.range.end)
.forEach(proc => {
const procDef = DocumentSymbol.create(
proc.name,
prettyKeywords(proc.keyword),
SymbolKind.Function,
Range.create(proc.range.start, 0, proc.range.end, 0),
Range.create(proc.range.start, 0, proc.range.start, 0),
Range.create(proc.range.start!, 0, proc.range.end!, 0),
Range.create(proc.range.start!, 0, proc.range.start!, 0),
);

if (proc.scope) {
Expand All @@ -35,8 +36,8 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
subitem.name,
prettyKeywords(subitem.keyword),
SymbolKind.Property,
Range.create(subitem.position.line, 0, subitem.position.line, 0),
Range.create(subitem.position.line, 0, subitem.position.line, 0)
Range.create(subitem.position.range.line, 0, subitem.position.range.line, 0),
Range.create(subitem.position.range.line, 0, subitem.position.range.line, 0)
));

procDef.children.push(...getScopeVars(proc.scope));
Expand All @@ -46,14 +47,14 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
});

currentScopeDefs.push(
...scope.subroutines.filter(sub => sub.position && sub.position.path === currentPath)
...scope.subroutines.filter(sub => sub.position && sub.position.path === currentPath && sub.range.start && sub.range.end)
.filter(def => def.range.start)
.map(def => DocumentSymbol.create(
def.name,
prettyKeywords(def.keyword),
SymbolKind.Function,
Range.create(def.range.start, 0, def.range.end, 0),
Range.create(def.range.start, 0, def.range.start, 0),
Range.create(def.range.start!, 0, def.range.end!, 0),
Range.create(def.range.start!, 0, def.range.start!, 0),
)),

...scope.variables
Expand All @@ -62,8 +63,8 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
def.name,
prettyKeywords(def.keyword),
SymbolKind.Variable,
Range.create(def.position.line, 0, def.position.line, 0),
Range.create(def.position.line, 0, def.position.line, 0)
Range.create(def.position.range.line, 0, def.position.range.line, 0),
Range.create(def.position.range.line, 0, def.position.range.line, 0)
))
);

Expand All @@ -74,8 +75,8 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
def.name,
prettyKeywords(def.keyword),
SymbolKind.Constant,
Range.create(def.position.line, 0, def.position.line, 0),
Range.create(def.position.line, 0, def.position.line, 0)
Range.create(def.position.range.line, 0, def.position.range.line, 0),
Range.create(def.position.range.line, 0, def.position.range.line, 0)
);

if (def.subItems.length > 0) {
Expand All @@ -85,8 +86,8 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
subitem.name,
prettyKeywords(subitem.keyword),
SymbolKind.Property,
Range.create(subitem.position.line, 0, subitem.position.line, 0),
Range.create(subitem.position.line, 0, subitem.position.line, 0)
Range.create(subitem.position.range.line, 0, subitem.position.range.line, 0),
Range.create(subitem.position.range.line, 0, subitem.position.range.line, 0)
));
}

Expand All @@ -100,8 +101,8 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
file.name,
prettyKeywords(file.keyword),
SymbolKind.File,
Range.create(file.position.line, 0, file.position.line, 0),
Range.create(file.position.line, 0, file.position.line, 0)
Range.create(file.position.range.line, 0, file.position.range.line, 0),
Range.create(file.position.range.line, 0, file.position.range.line, 0)
);

fileDef.children = [];
Expand All @@ -113,8 +114,8 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
recordFormat.name,
prettyKeywords(recordFormat.keyword),
SymbolKind.Struct,
Range.create(recordFormat.position.line, 0, recordFormat.position.line, 0),
Range.create(recordFormat.position.line, 0, recordFormat.position.line, 0)
Range.create(recordFormat.position.range.line, 0, recordFormat.position.range.line, 0),
Range.create(recordFormat.position.range.line, 0, recordFormat.position.range.line, 0)
);

recordFormatDef.children = recordFormat.subItems
Expand All @@ -123,8 +124,8 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
subitem.name,
prettyKeywords(subitem.keyword),
SymbolKind.Property,
Range.create(subitem.position.line, 0, subitem.position.line, 0),
Range.create(subitem.position.line, 0, subitem.position.line, 0)
Range.create(subitem.position.range.line, 0, subitem.position.range.line, 0),
Range.create(subitem.position.range.line, 0, subitem.position.range.line, 0)
));

if (fileDef.children) {
Expand All @@ -136,14 +137,14 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
});

scope.structs
.filter(struct => struct.position && struct.position.path === currentPath)
.filter(struct => struct.position && struct.position.path === currentPath && struct.range.start && struct.range.end)
.forEach(struct => {
const structDef = DocumentSymbol.create(
struct.name,
prettyKeywords(struct.keyword),
SymbolKind.Struct,
Range.create(struct.range.start, 0, struct.range.end, 0),
Range.create(struct.range.start, 0, struct.range.start, 0),
Range.create(struct.range.start!, 0, struct.range.end!, 0),
Range.create(struct.range.start!, 0, struct.range.start!, 0),
);

structDef.children = struct.subItems
Expand All @@ -152,8 +153,8 @@ export default async function documentSymbolProvider(handler: DocumentSymbolPara
subitem.name,
prettyKeywords(subitem.keyword),
SymbolKind.Property,
Range.create(subitem.position.line, 0, subitem.position.line, 0),
Range.create(subitem.position.line, 0, subitem.position.line, 0)
Range.create(subitem.position.range.line, 0, subitem.position.range.line, 0),
Range.create(subitem.position.range.line, 0, subitem.position.range.line, 0)
));

currentScopeDefs.push(structDef);
Expand Down
6 changes: 3 additions & 3 deletions extension/server/src/providers/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default async function hoverProvider(params: HoverParams): Promise<Hover|
}

if (procedure.position && currentPath !== procedure.position.path) {
markdown += `\n\n*@file* \`${procedure.position.path}:${procedure.position.line+1}\``;
markdown += `\n\n*@file* \`${procedure.position.path}:${procedure.position.range.line+1}\``;
}

return {
Expand All @@ -70,7 +70,7 @@ export default async function hoverProvider(params: HoverParams): Promise<Hover|
};
} else {
// If they're inside of a procedure, let's get the stuff from there too
const currentProcedure = doc.procedures.find(proc => currentLine >= proc.range.start && currentLine <= proc.range.end);
const currentProcedure = doc.procedures.find(proc => proc.range.start && proc.range.end && currentLine >= proc.range.start && currentLine <= proc.range.end);
let theVariable;

if (currentProcedure && currentProcedure.scope) {
Expand All @@ -88,7 +88,7 @@ export default async function hoverProvider(params: HoverParams): Promise<Hover|
let markdown = `\`${theVariable.name} ${prettyKeywords(theVariable.keyword)}\` (${refs} reference${refs === 1 ? `` : `s`})`;

if (theVariable.position && currentPath !== theVariable.position.path) {
markdown += `\n\n*@file* \`${theVariable.position.path}:${theVariable.position.line+1}\``;
markdown += `\n\n*@file* \`${theVariable.position.path}:${theVariable.position.range.line+1}\``;
}

return {
Expand Down
4 changes: 2 additions & 2 deletions extension/server/src/providers/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export default async function implementationProvider(params: ImplementationParam
return Location.create(
proc.position.path,
Range.create(
proc.position.line,
proc.position.range.line,
0,
proc.position.line,
proc.position.range.line,
0
)
);
Expand Down
2 changes: 1 addition & 1 deletion extension/server/src/providers/linter/codeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default async function codeActionsProvider(params: CodeActionParams): Pro
const detail = await refreshLinterDiagnostics(document, docs, false);
if (detail) {
const fixErrors = detail.errors.filter(error =>
range.start.line >= document.positionAt(error.offset.position!).line &&
range.start.line >= document.positionAt(error.offset.start!).line &&
range.end.line <= document.positionAt(error.offset.end!).line
);

Expand Down
28 changes: 14 additions & 14 deletions extension/server/src/providers/linter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function calculateOffset(document: TextDocument, error: IssueRange) {
const offset = error.offset;

return Range.create(
document.positionAt(error.offset.position),
document.positionAt(error.offset.start),
document.positionAt(error.offset.end)
);
};
Expand Down Expand Up @@ -435,17 +435,17 @@ export function getActions(document: TextDocument, errors: IssueRange[]) {

export function getSubroutineActions(document: TextDocument, docs: Cache, range: Range): CodeAction|undefined {
if (range.start.line === range.end.line) {
const currentGlobalSubroutine = docs.subroutines.find(sub => sub.position.line === range.start.line);
const currentGlobalSubroutine = docs.subroutines.find(sub => sub.position.range.line === range.start.line && sub.range.start && sub.range.end);

if (currentGlobalSubroutine) {
const subroutineRange = Range.create(
Position.create(currentGlobalSubroutine.range.start, 0),
Position.create(currentGlobalSubroutine.range.end, 1000)
Position.create(currentGlobalSubroutine.range.start!, 0),
Position.create(currentGlobalSubroutine.range.end!, 1000)
);

const bodyRange = Range.create(
Position.create(currentGlobalSubroutine.range.start + 1, 0),
Position.create(currentGlobalSubroutine.range.end - 1, 0)
Position.create(currentGlobalSubroutine.range.start! + 1, 0),
Position.create(currentGlobalSubroutine.range.end! - 1, 0)
);

// First, let's create the extract data
Expand All @@ -464,13 +464,13 @@ export function getSubroutineActions(document: TextDocument, docs: Cache, range:

// Then update the references that invokes this subroutine
const referenceUpdates: TextEdit[] = currentGlobalSubroutine.references.map(ref => {
const lineNumber = document.positionAt(ref.offset.position).line;
const lineNumber = document.positionAt(ref.offset.start).line;
// If this reference is outside of the subroutine
if (lineNumber < currentGlobalSubroutine.range.start || lineNumber > currentGlobalSubroutine.range.end) {
if (lineNumber < currentGlobalSubroutine.range.start! || lineNumber > currentGlobalSubroutine.range.end!) {
return TextEdit.replace(
Range.create(
// - 5 `EXSR `
document.positionAt(ref.offset.position - 5),
document.positionAt(ref.offset.start - 5),
document.positionAt(ref.offset.end)
),
currentGlobalSubroutine.name + `(${extracted.references.map(r => r.dec.name).join(`:`)})`
Expand Down Expand Up @@ -538,7 +538,7 @@ function caseInsensitiveReplaceAll(text: string, search: string, replace: string

function createExtract(document: TextDocument, userRange: Range, docs: Cache) {
const range = Range.create(userRange.start.line, 0, userRange.end.line, 1000);
const references = docs.referencesInRange(document.uri, {position: document.offsetAt(range.start), end: document.offsetAt(range.end)});
const references = docs.referencesInRange(document.uri, {start: document.offsetAt(range.start), end: document.offsetAt(range.end)});
const validRefs = references.filter(ref => [`struct`, `subitem`, `variable`].includes(ref.dec.type));

const nameDiffSize = 1; // Always once since we only add 'p' at the start
Expand All @@ -551,7 +551,7 @@ function createExtract(document: TextDocument, userRange: Range, docs: Cache) {
for (let i = validRefs.length - 1; i >= 0; i--) {
for (let y = validRefs[i].refs.length - 1; y >= 0; y--) {
validRefs[i].refs[y] = {
position: validRefs[i].refs[y].position - rangeStartOffset,
start: validRefs[i].refs[y].start - rangeStartOffset,
end: validRefs[i].refs[y].end - rangeStartOffset
};
}
Expand All @@ -562,15 +562,15 @@ function createExtract(document: TextDocument, userRange: Range, docs: Cache) {
for (let y = validRefs[i].refs.length - 1; y >= 0; y--) {
const ref = validRefs[i].refs[y];

newBody = newBody.slice(0, ref.position) + newParamNames[i] + newBody.slice(ref.end);
newBody = newBody.slice(0, ref.start) + newParamNames[i] + newBody.slice(ref.end);
ref.end += nameDiffSize;

// Then we need to update the offset of the next references
for (let z = i - 1; z >= 0; z--) {
for (let x = validRefs[z].refs.length - 1; x >= 0; x--) {
if (validRefs[z].refs[x].position > ref.end) {
if (validRefs[z].refs[x].start > ref.end) {
validRefs[z].refs[x] = {
position: validRefs[z].refs[x].position + nameDiffSize,
start: validRefs[z].refs[x].start + nameDiffSize,
end: validRefs[z].refs[x].end + nameDiffSize
};
}
Expand Down
4 changes: 2 additions & 2 deletions extension/server/src/providers/project/workspaceSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export default function workspaceSymbolProvider(params: WorkspaceSymbolParams):
SymbolKind.Function,
uri,
Range.create(
proc.position.line,
proc.position.range.line,
0,
proc.position.line,
proc.position.range.line,
0
)
)
Expand Down
6 changes: 3 additions & 3 deletions extension/server/src/providers/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export async function renamePrepareProvider(params: PrepareRenameParams): Promis
return;
}

const currentSelectedRef = def?.references.find(r => document.positionAt(r.offset.position).line === currentPos.line);
const currentSelectedRef = def?.references.find(r => document.positionAt(r.offset.start).line === currentPos.line);

if (currentSelectedRef) {
return Range.create(
document.positionAt(currentSelectedRef.offset.position),
document.positionAt(currentSelectedRef.offset.start),
document.positionAt(currentSelectedRef.offset.end)
)
}
Expand Down Expand Up @@ -60,7 +60,7 @@ export async function renameRequestProvider(params: RenameParams): Promise<Works
edits[uri] = def.references.filter(ref => ref.uri === uri).map(ref => ({
newText: params.newName,
range: Range.create(
document.positionAt(ref.offset.position),
document.positionAt(ref.offset.start),
document.positionAt(ref.offset.end)
)
}));
Expand Down

0 comments on commit 71b6491

Please sign in to comment.