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

Wip/ts null checks #1568

Merged
merged 7 commits into from
Mar 6, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changes to Calva.

## [Unreleased]
- Maintenance: [Begin work on enabling strictNullChecks in the TypeScript config.](https://github.com/BetterThanTomorrow/calva/pull/1568)

## [2.0.252] - 2022-03-05
- Fix: [Tab doesn't work in snippet mode](https://github.com/BetterThanTomorrow/calva/pull/1580)
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2600,7 +2600,8 @@
"clean": "rimraf ./out && rimraf ./tsconfig.tsbuildinfo && rimraf ./cljs-out",
"compile-cljs": "npx shadow-cljs compile :calva-lib :test",
"prewatch": "npm i && npm run clean && npm run update-grammar && npm run compile-cljs",
"watch": "npx tsc -watch -p ./tsconfig.json",
"watch": "npx tsc --watch --project ./tsconfig.json",
"watch-with-strict-nulls": "npx tsc --watch --project ./tsconfig.json --strictNullChecks",
corasaurus-hex marked this conversation as resolved.
Show resolved Hide resolved
"release-cljs": "npx shadow-cljs release :calva-lib :test",
"update-grammar": "node ./src/calva-fmt/update-grammar.js ./src/calva-fmt/atom-language-clojure/grammars/clojure.cson clojure.tmLanguage.json",
"release": "npm i && npm run clean && npm run update-grammar && npm run release-cljs && webpack --mode production",
Expand Down Expand Up @@ -2687,4 +2688,4 @@
"webpack": "^5.27.1",
"webpack-cli": "^4.5.0"
}
}
}
11 changes: 6 additions & 5 deletions src/calva-fmt/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as config from './config';
import * as outputWindow from '../../results-output/results-doc';
import {
getIndent,
getDocument,
getDocumentOffset,
MirroredDocument,
mustGetDocument,
} from '../../doc-mirror/index';
import {
formatTextAtRange,
Expand All @@ -15,15 +15,16 @@ import {
cljify,
jsify,
} from '../../../out/cljs-lib/cljs-lib';
import * as util from '../../utilities';

export async function indentPosition(
position: vscode.Position,
document: vscode.TextDocument
) {
const editor = vscode.window.activeTextEditor;
const editor = util.mustGetActiveTextEditor();
const pos = new vscode.Position(position.line, 0);
const indent = getIndent(
getDocument(document).model.lineInputModel,
mustGetDocument(document).model.lineInputModel,
getDocumentOffset(document, position),
await config.getConfig()
);
Expand Down Expand Up @@ -55,7 +56,7 @@ export async function formatRangeEdits(
range: vscode.Range
): Promise<vscode.TextEdit[]> {
const text: string = document.getText(range);
const mirroredDoc: MirroredDocument = getDocument(document);
const mirroredDoc: MirroredDocument = mustGetDocument(document);
const startIndex = document.offsetAt(range.start);
const endIndex = document.offsetAt(range.end);
const cursor = mirroredDoc.getTokenCursor(startIndex);
Expand Down Expand Up @@ -90,7 +91,7 @@ export async function formatPositionInfo(
const doc: vscode.TextDocument = editor.document;
const pos: vscode.Position = editor.selection.active;
const index = doc.offsetAt(pos);
const mirroredDoc: MirroredDocument = getDocument(doc);
const mirroredDoc: MirroredDocument = mustGetDocument(doc);
const cursor = mirroredDoc.getTokenCursor(index);
const formatDepth = extraConfig['format-depth']
? extraConfig['format-depth']
Expand Down
6 changes: 4 additions & 2 deletions src/calva-fmt/src/providers/ontype_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as docMirror from '../../../doc-mirror/index';
import { EditableDocument } from '../../../cursor-doc/model';
import * as paredit from '../../../cursor-doc/paredit';
import { getConfig } from '../../../config';
import * as util from '../../../utilities';

export class FormatOnTypeEditProvider
implements vscode.OnTypeFormattingEditProvider
Expand All @@ -14,7 +15,6 @@ export class FormatOnTypeEditProvider
ch: string,
_options
): Promise<vscode.TextEdit[]> {
const editor = vscode.window.activeTextEditor;
let keyMap = vscode.workspace
.getConfiguration()
.get('calva.paredit.defaultKeyMap');
Expand All @@ -24,7 +24,8 @@ export class FormatOnTypeEditProvider
keyMap === 'strict' &&
getConfig().strictPreventUnmatchedClosingBracket
) {
const mDoc: EditableDocument = docMirror.getDocument(document);
const mDoc: EditableDocument =
docMirror.mustGetDocument(document);
const tokenCursor = mDoc.getTokenCursor();
if (tokenCursor.withinComment()) {
return null;
Expand All @@ -37,6 +38,7 @@ export class FormatOnTypeEditProvider
return null;
}
}
const editor = util.mustGetActiveTextEditor();

const pos = editor.selection.active;
if (
Expand Down
4 changes: 2 additions & 2 deletions src/clojuredocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function printTextToRichCommentCommand(args: { [x: string]: string }) {

function printTextToRichComment(text: string, position?: number) {
const doc = util.getDocument({});
const mirrorDoc = docMirror.getDocument(doc);
const mirrorDoc = docMirror.mustGetDocument(doc);
paredit.addRichComment(
mirrorDoc,
position ? position : mirrorDoc.selection.active,
Expand Down Expand Up @@ -183,7 +183,7 @@ async function clojureDocsLookup(
p?: vscode.Position
): Promise<DocsEntry> {
const doc = d ? d : util.getDocument({});
const position = p ? p : vscode.window.activeTextEditor.selection.active;
const position = p ? p : util.mustGetActiveTextEditor().selection.active;
const symbol = util.getWordAtPosition(doc, position);
const ns = namespace.getNamespace(doc);
const session = replSession.getSession(util.getFileType(doc));
Expand Down
2 changes: 1 addition & 1 deletion src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ export default {
setStateValue('cljc', newSession);
if (
outputWindow.isResultsDoc(
vscode.window.activeTextEditor.document
util.mustGetActiveTextEditor().document
)
) {
outputWindow.setSession(newSession, undefined);
Expand Down
2 changes: 1 addition & 1 deletion src/custom-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function evaluateCustomCodeSnippetCommand(codeOrKey?: string) {
}

async function evaluateCodeOrKey(codeOrKey?: string) {
const editor = vscode.window.activeTextEditor;
const editor = util.mustGetActiveTextEditor();
const currentLine = editor.selection.active.line;
const currentColumn = editor.selection.active.character;
const currentFilename = editor.document.fileName;
Expand Down
2 changes: 1 addition & 1 deletion src/debugger/calva-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class CalvaDebugSession extends LoggingDebugSession {
new Position(positionLine, positionColumn)
);
const tokenCursor = docMirror
.getDocument(document)
.mustGetDocument(document)
.getTokenCursor(offset);

try {
Expand Down
4 changes: 2 additions & 2 deletions src/debugger/decorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function triggerUpdateAndRenderDecorations() {
timeout = undefined;
}
if (enabled) {
const editor = vscode.window.activeTextEditor;
const editor = util.getActiveTextEditor();
if (editor) {
timeout = setTimeout(() => {
const cljSession = replSession.getSession('clj');
Expand All @@ -166,7 +166,7 @@ function activate() {
});

vscode.workspace.onDidChangeTextDocument((event) => {
const activeEditor = vscode.window.activeTextEditor;
const activeEditor = util.getActiveTextEditor();
if (
activeEditor &&
event.document === activeEditor.document &&
Expand Down
31 changes: 21 additions & 10 deletions src/doc-mirror/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
LineInputModel,
ModelEditSelection,
} from '../cursor-doc/model';
import { isUndefined } from 'lodash';

const documents = new Map<vscode.TextDocument, MirroredDocument>();

Expand All @@ -28,7 +29,7 @@ export class DocumentModel implements EditableModel {
modelEdits: ModelEdit[],
options: ModelEditOptions
): Thenable<boolean> {
const editor = vscode.window.activeTextEditor,
const editor = utilities.mustGetActiveTextEditor(),
undoStopBefore = !!options.undoStopBefore;
return editor
.edit(
Expand Down Expand Up @@ -84,7 +85,7 @@ export class DocumentModel implements EditableModel {
oldSelection?: [number, number],
newSelection?: [number, number]
) {
const editor = vscode.window.activeTextEditor,
const editor = utilities.mustGetActiveTextEditor(),
document = editor.document;
builder.insert(document.positionAt(offset), text);
}
Expand All @@ -97,7 +98,7 @@ export class DocumentModel implements EditableModel {
oldSelection?: [number, number],
newSelection?: [number, number]
) {
const editor = vscode.window.activeTextEditor,
const editor = utilities.mustGetActiveTextEditor(),
document = editor.document,
range = new vscode.Range(
document.positionAt(start),
Expand All @@ -113,7 +114,7 @@ export class DocumentModel implements EditableModel {
oldSelection?: [number, number],
newSelection?: [number, number]
) {
const editor = vscode.window.activeTextEditor,
const editor = utilities.mustGetActiveTextEditor(),
document = editor.document,
range = new vscode.Range(
document.positionAt(offset),
Expand Down Expand Up @@ -143,13 +144,13 @@ export class MirroredDocument implements EditableDocument {

get selectionLeft(): number {
return this.document.offsetAt(
vscode.window.activeTextEditor.selection.anchor
utilities.mustGetActiveTextEditor().selection.anchor
);
}

get selectionRight(): number {
return this.document.offsetAt(
vscode.window.activeTextEditor.selection.active
utilities.mustGetActiveTextEditor().selection.active
);
}

Expand All @@ -165,7 +166,7 @@ export class MirroredDocument implements EditableDocument {
}

public insertString(text: string) {
const editor = vscode.window.activeTextEditor,
const editor = utilities.mustGetActiveTextEditor(),
selection = editor.selection,
wsEdit = new vscode.WorkspaceEdit(),
edit = vscode.TextEdit.insert(
Expand All @@ -179,7 +180,7 @@ export class MirroredDocument implements EditableDocument {
}

set selection(selection: ModelEditSelection) {
const editor = vscode.window.activeTextEditor,
const editor = utilities.mustGetActiveTextEditor(),
document = editor.document,
anchor = document.positionAt(selection.anchor),
active = document.positionAt(selection.active);
Expand All @@ -192,7 +193,7 @@ export class MirroredDocument implements EditableDocument {
}

public getSelectionText() {
const editor = vscode.window.activeTextEditor,
const editor = utilities.mustGetActiveTextEditor(),
selection = editor.selection;
return this.document.getText(selection);
}
Expand Down Expand Up @@ -241,11 +242,21 @@ export function getDocument(doc: vscode.TextDocument) {
return documents.get(doc);
}

export function mustGetDocument(doc: vscode.TextDocument) {
const mirrorDoc = documents.get(doc);

if (isUndefined(mirrorDoc)) {
throw new Error('Missing mirror document!');
}

return mirrorDoc;
}

export function getDocumentOffset(
doc: vscode.TextDocument,
position: vscode.Position
) {
const model = getDocument(doc).model;
const model = mustGetDocument(doc).model;
return model.getOffsetForLine(position.line) + position.character;
}

Expand Down
4 changes: 2 additions & 2 deletions src/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import * as docMirror from './doc-mirror/index';
export function continueCommentCommand() {
const document = util.getDocument({});
if (document && document.languageId === 'clojure') {
const editor = vscode.window.activeTextEditor;
const editor = util.mustGetActiveTextEditor();
const position = editor.selection.active;
const cursor = docMirror.getDocument(document).getTokenCursor();
const cursor = docMirror.mustGetDocument(document).getTokenCursor();
if (cursor.getToken().type !== 'comment') {
if (cursor.getPrevToken().type === 'comment') {
cursor.previous();
Expand Down
4 changes: 2 additions & 2 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async function evaluateCode(
const filePath = options.filePath;
const session: NReplSession = options.session;
const ns = options.ns;
const editor = vscode.window.activeTextEditor;
const editor = util.mustGetActiveTextEditor();
let result = null;

if (code.length > 0) {
Expand Down Expand Up @@ -259,7 +259,7 @@ async function evaluateSelection(document = {}, options) {
) => [vscode.Selection, string] = options.selectionFn;

if (getStateValue('connected')) {
const editor = vscode.window.activeTextEditor;
const editor = util.mustGetActiveTextEditor();
state.analytics().logEvent('Evaluation', 'selectionFn').send();
const selection = selectionFn(editor);
const codeSelection: vscode.Selection = selection[0];
Expand Down
4 changes: 2 additions & 2 deletions src/extension-test/integration/suite/extension-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as vscode from 'vscode';
// import * as myExtension from '../extension';
import * as outputWindow from '../../../results-output/results-doc';
import { commands } from 'vscode';
import { getDocument } from '../../../doc-mirror';
import { mustGetDocument } from '../../../doc-mirror';

void vscode.window.showInformationMessage('Tests running. Yay!');

Expand Down Expand Up @@ -85,7 +85,7 @@ suite('Extension Test Suite', () => {
await sleep(500); // wait a little longer for repl output to be done
console.log('connected to repl');

const resultsDoc = getDocument(await outputWindow.openResultsDoc());
const resultsDoc = mustGetDocument(await outputWindow.openResultsDoc());

// focus the clojure file
await vscode.workspace.openTextDocument(testUri).then((doc) =>
Expand Down
3 changes: 2 additions & 1 deletion src/file-switcher/file-switcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import * as path from 'path';
import * as util from './util';
import * as projectRoot from '../project-root';
import { mustGetActiveTextEditor } from '../utilities';

function openFile(file) {
return vscode.workspace
Expand Down Expand Up @@ -39,7 +40,7 @@ function askToCreateANewFile(dir, file) {
}

export async function toggleBetweenImplAndTest() {
const activeFile = vscode.window.activeTextEditor;
const activeFile = mustGetActiveTextEditor();
const openedFilename = activeFile.document.fileName;

const projectRootUri = await projectRoot.getProjectRootUri();
Expand Down
11 changes: 6 additions & 5 deletions src/highlight/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isArray } from 'util';
import * as docMirror from '../../doc-mirror/index';
import { Token, validPair } from '../../cursor-doc/clojure-lexer';
import { LispTokenCursor } from '../../cursor-doc/token-cursor';
import { getActiveTextEditor, mustGetActiveTextEditor } from '../../utilities';

type StackItem = {
char: string;
Expand Down Expand Up @@ -264,7 +265,7 @@ function updateRainbowBrackets() {
}

const doc = activeEditor.document,
mirrorDoc = docMirror.getDocument(doc),
mirrorDoc = docMirror.mustGetDocument(doc),
rainbow = rainbowTypes.map(() => []),
rainbowGuides = rainbowTypes.map(() => []),
misplaced = [],
Expand Down Expand Up @@ -540,15 +541,15 @@ function decorateGuide(

function decorateActiveGuides() {
const activeGuides = [];
activeEditor = vscode.window.activeTextEditor;
activeEditor = mustGetActiveTextEditor();
if (activeGuidesTypes) {
activeGuidesTypes.forEach((type) =>
activeEditor.setDecorations(type, [])
);
}
activeEditor.selections.forEach((selection) => {
const doc = activeEditor.document;
const mirrorDoc = docMirror.getDocument(doc);
const mirrorDoc = docMirror.mustGetDocument(doc);
const cursor = mirrorDoc.getTokenCursor(doc.offsetAt(selection.start));
const visitedEndPositions = [selection.start];
findActiveGuide: while (cursor.forwardList() && cursor.upList()) {
Expand Down Expand Up @@ -585,7 +586,7 @@ function decorateActiveGuides() {
}

export function activate(context: vscode.ExtensionContext) {
activeEditor = vscode.window.activeTextEditor;
activeEditor = mustGetActiveTextEditor();

vscode.window.onDidChangeActiveTextEditor(
(editor) => {
Expand All @@ -601,7 +602,7 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.onDidChangeTextEditorSelection(
(event) => {
if (
event.textEditor === vscode.window.activeTextEditor &&
event.textEditor === getActiveTextEditor() &&
is_clojure(event.textEditor)
) {
if (lastHighlightedEditor !== event.textEditor) {
Expand Down
Loading