Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment/change span to range #43

Merged
merged 4 commits into from
Aug 24, 2024
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
86 changes: 29 additions & 57 deletions src/cli/commands/lspCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
_Connection,
createConnection,
} from "vscode-languageserver";
import { TextDocument, Range } from "vscode-languageserver-textdocument";
import { TextDocument } from "vscode-languageserver-textdocument";

Check warning on line 16 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L16

Added line #L16 was not covered by tests
import {
AntlrLexerError,
AntlrParsingError,
Span,
UntypedModule,
parse,
} from "../../parser";
Expand Down Expand Up @@ -63,7 +62,7 @@
message: err.description,
source: "Parsing",
severity: DiagnosticSeverity.Error,
range: spannedToRange(document, err.span),
range: err.range,

Check warning on line 65 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L65

Added line #L65 was not covered by tests
},
],
};
Expand All @@ -89,19 +88,14 @@
document: TextDocument,
err: AntlrLexerError,
): PublishDiagnosticsParams {
const position: Position = {
character: err.column,
line: err.line,
};

return {
uri: document.uri,
diagnostics: [
{
message: err.description,
source: "Parsing",
severity: DiagnosticSeverity.Error,
range: { start: position, end: position },
range: { start: err.position, end: err.position },

Check warning on line 98 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L98

Added line #L98 was not covered by tests
},
],
};
Expand All @@ -120,7 +114,7 @@
`${e.description.errorName}\n\n${e.description.shortDescription()}`,
),
severity: toDiagnosticSeverity(e.description.severity),
range: spannedToRange(document, e.span),
range: e.range,

Check warning on line 117 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L117

Added line #L117 was not covered by tests
})),
};
}
Expand Down Expand Up @@ -305,12 +299,12 @@
if (module?.typed === undefined) {
return;
}
return getInlayHints(module.typed, module.document).map(
return getInlayHints(module.typed).map(

Check warning on line 302 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L302

Added line #L302 was not covered by tests
(hint): InlayHint => ({
label: hint.label,
paddingLeft: hint.paddingLeft,
kind: InlayHintKind.Type,
position: module.document.positionAt(hint.offset),
position: hint.positition,

Check warning on line 307 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L307

Added line #L307 was not covered by tests
}),
);
});
Expand All @@ -336,9 +330,7 @@
return;
}

const offset = module.document.offsetAt(position);

const hint = functionSignatureHint(module.typed, offset);
const hint = functionSignatureHint(module.typed, position);

Check warning on line 333 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L333

Added line #L333 was not covered by tests

if (hint === undefined) {
return;
Expand Down Expand Up @@ -369,9 +361,7 @@
return;
}

const offset = module.document.offsetAt(position);

const kind = getCompletionItems(module.typed, offset, {
const kind = getCompletionItems(module.typed, position, {

Check warning on line 364 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L364

Added line #L364 was not covered by tests
getModuleByNs(ns) {
return state.moduleByNs(ns)?.typed;
},
Expand All @@ -392,18 +382,14 @@
}

const refs =
findReferences(
ns,
module.document.offsetAt(position),
state.getTypedProject(),
)?.references ?? [];
findReferences(ns, position, state.getTypedProject())?.references ?? [];

Check warning on line 385 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L385

Added line #L385 was not covered by tests

return refs.map(([referenceNs, referenceExpr]) => {
const referenceModule = state.moduleByNs(referenceNs)!;

return {
uri: referenceModule.document.uri,
range: spannedToRange(referenceModule.document, referenceExpr.span),
range: referenceExpr.range,

Check warning on line 392 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L392

Added line #L392 was not covered by tests
};
});
});
Expand All @@ -423,11 +409,7 @@
return;
}

const refs = findReferences(
ns,
module.document.offsetAt(position),
state.getTypedProject(),
);
const refs = findReferences(ns, position, state.getTypedProject());

Check warning on line 412 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L412

Added line #L412 was not covered by tests

if (refs === undefined) {
return;
Expand All @@ -440,10 +422,7 @@

getOrDefault(changes, module.document.uri, []).push({
newText: newName,
range: spannedToRange(
module.document,
refs.resolution.declaration.binding.span,
),
range: refs.resolution.declaration.binding.range,

Check warning on line 425 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L425

Added line #L425 was not covered by tests
});

break;
Expand All @@ -467,7 +446,7 @@

getOrDefault(changes, refModule.document.uri, []).push({
newText,
range: spannedToRange(refModule.document, ident.span),
range: ident.range,

Check warning on line 449 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L449

Added line #L449 was not covered by tests
});
}

Expand All @@ -486,12 +465,14 @@
}

const formatted_ = format(module.untyped);

const start: Position = { line: 0, character: 0 };
const len = module.document.getText().length;
const end = module.document.positionAt(len);

Check warning on line 472 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L468-L472

Added lines #L468 - L472 were not covered by tests
return [
{
range: spannedToRange(module.document, [
0,
module.document.getText().length,
]),
range: { start, end },

Check warning on line 475 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L475

Added line #L475 was not covered by tests
newText: formatted_,
} as TextEdit,
];
Expand All @@ -509,18 +490,18 @@

const decls = module.typed.declarations.map((st) => ({
name: st.binding.name,
span: st.span,
range: st.range,

Check warning on line 493 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L493

Added line #L493 was not covered by tests
}));
const typeDecl = module.typed.typeDeclarations.map((st) => ({
name: st.name,
span: st.span,
range: st.range,

Check warning on line 497 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L497

Added line #L497 was not covered by tests
}));
return decls.concat(typeDecl).map(({ span, name }) => ({
return decls.concat(typeDecl).map(({ range, name }) => ({

Check warning on line 499 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L499

Added line #L499 was not covered by tests
kind: SymbolKind.Variable,
name,
location: {
uri: textDocument.uri,
range: spannedToRange(module.document, span),
range,

Check warning on line 504 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L504

Added line #L504 was not covered by tests
},
}));
});
Expand All @@ -531,11 +512,11 @@
return;
}

return module.typed.declarations.map(({ span, binding, scheme }) => {
return module.typed.declarations.map(({ range, binding, scheme }) => {

Check warning on line 515 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L515

Added line #L515 was not covered by tests
const tpp = typeToString(binding.$.asType(), scheme);
return {
command: { title: tpp, command: "noop" },
range: spannedToRange(module.document, span),
range,

Check warning on line 519 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L519

Added line #L519 was not covered by tests
};
});
});
Expand All @@ -548,8 +529,7 @@
return;
}

const offset = module.document.offsetAt(position);
const resolved = goToDefinitionOf(module.typed, offset);
const resolved = goToDefinitionOf(module.typed, position);

Check warning on line 532 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L532

Added line #L532 was not covered by tests
if (resolved === undefined) {
return undefined;
}
Expand All @@ -561,7 +541,7 @@

return {
uri: definitionDoc.uri,
range: spannedToRange(definitionDoc, resolved.span),
range: resolved.range,

Check warning on line 544 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L544

Added line #L544 was not covered by tests
};
});

Expand All @@ -571,16 +551,15 @@
return;
}

const offset = module.document.offsetAt(position);
const hoverData = hoverOn(module.ns, module.typed, offset);
const hoverData = hoverOn(module.ns, module.typed, position);

Check warning on line 554 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L554

Added line #L554 was not covered by tests
if (hoverData === undefined) {
return undefined;
}

const [scheme, hover] = hoverData;
const md = hoverToMarkdown(scheme, hover);
return {
range: spannedToRange(module.document, hover.span),
range: hover.range,

Check warning on line 562 in src/cli/commands/lspCmd.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/commands/lspCmd.ts#L562

Added line #L562 was not covered by tests
contents: {
kind: MarkupKind.Markdown,
value: md,
Expand All @@ -592,13 +571,6 @@
connection.listen();
}

function spannedToRange(doc: TextDocument, [start, end]: Span): Range {
return {
start: doc.positionAt(start),
end: doc.positionAt(end),
};
}

function toDiagnosticSeverity(severity: Severity): DiagnosticSeverity {
switch (severity) {
case "error":
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function foldInfix(
return {
type: "constant",
value: fold(left.value, right.value),
span: src.span,
range: src.range,
$: src.$,
};
}
Expand Down Expand Up @@ -93,7 +93,7 @@ const iifFolding: Optimization = (src) => {
return zipped.reduceRight(
(acc, [pattern, arg]): TypedExpr => ({
type: "let",
span: src.span,
range: src.range,
$: src.$,
pattern,
body: acc,
Expand Down
15 changes: 11 additions & 4 deletions src/documentation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TypedProject } from "./cli/common";
import { Position } from "./parser";
import { TypedModule, TypedTypeAst, typeToString } from "./typecheck";
import { gtEqPos } from "./typecheck/typedAst/common";

export type Variant = {
name: string;
Expand Down Expand Up @@ -52,7 +54,7 @@ export function makeModuleDoc(
): ModuleDoc {
const items: Item[] = [];

const indexes = new WeakMap<Item, number>();
const positions = new WeakMap<Item, Position>();

for (const decl of typedModule.declarations) {
if (!decl.pub) {
Expand All @@ -69,7 +71,7 @@ export function makeModuleDoc(
item.docComment = decl.docComment;
}

indexes.set(item, decl.span[0]);
positions.set(item, decl.range.start);
items.push(item);
}

Expand Down Expand Up @@ -99,11 +101,16 @@ export function makeModuleDoc(
}));
}

indexes.set(item, typeDecl.span[0]);
positions.set(item, typeDecl.range.start);
items.push(item);
}

items.sort((a, b) => indexes.get(a)! - indexes.get(b)!);
items.sort((a, b) => {
if (gtEqPos(positions.get(a)!, positions.get(b)!)) {
return 1;
}
return -1;
});

const moduleDoc: ModuleDoc = {
moduleName,
Expand Down
8 changes: 4 additions & 4 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { showErrorLine } from "./errors/showErrorLine";
import { Span } from "./parser";
import { Range } from "./parser";
import { Type, typeToString } from "./typecheck";
import { col, withDisabled } from "./utils/colors";

export type Severity = "error" | "warning";

export type ErrorInfo = {
span: Span;
range: Range;
description: ErrorDescription;
};

Expand Down Expand Up @@ -302,7 +302,7 @@ export class MissingRequiredFields implements ErrorDescription {

export function errorInfoToString(
src: string,
{ description, span }: ErrorInfo,
{ description, range }: ErrorInfo,
disableColors: boolean = false,
): string {
return withDisabled(disableColors, () => {
Expand All @@ -314,6 +314,6 @@ export function errorInfoToString(

${description.shortDescription()}

${showErrorLine(src, span)}`;
${showErrorLine(src, range)}`;
});
}
Loading
Loading