-
Notifications
You must be signed in to change notification settings - Fork 463
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
added new command to create new SQL query document #569
Changes from all commits
f3bed39
dd4f8fa
1ffbcf8
007a8b5
21a0c55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
|
||
import VscodeWrapper from './vscodeWrapper'; | ||
import vscode = require('vscode'); | ||
import path = require('path'); | ||
import os = require('os'); | ||
const fs = require('fs'); | ||
|
||
/** | ||
* Service for creating untitled documents for SQL query | ||
*/ | ||
export default class UntitledSqlDocumentService { | ||
private _counter: number = 1; | ||
|
||
constructor(private vscodeWrapper: VscodeWrapper) { | ||
} | ||
|
||
/** | ||
* Creates new untitled document for SQL query and opens in new editor tab | ||
*/ | ||
public newQuery(): Promise<boolean> { | ||
|
||
return new Promise<boolean>((resolve, reject) => { | ||
try { | ||
let filePath = this.createUntitledFilePath(); | ||
let docUri: vscode.Uri = vscode.Uri.parse('untitled:' + filePath); | ||
|
||
// Open an untitled document. So the file doesn't have to exist in disk | ||
this.vscodeWrapper.openTextDocument(docUri).then(doc => { | ||
// Show the new untitled document in the editor's first tab and change the focus to it. | ||
this.vscodeWrapper.showTextDocument(doc, 1, false).then(textDoc => { | ||
this._counter++; | ||
resolve(true); | ||
}); | ||
}); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
} | ||
|
||
private createUntitledFilePath(): string { | ||
let filePath = UntitledSqlDocumentService.createFilePath(this._counter); | ||
while (fs.existsSync(filePath)) { | ||
this._counter++; | ||
filePath = UntitledSqlDocumentService.createFilePath(this._counter); | ||
} | ||
while (this.vscodeWrapper.textDocuments.find(x => x.fileName.toUpperCase() === filePath.toUpperCase())) { | ||
this._counter++; | ||
filePath = UntitledSqlDocumentService.createFilePath(this._counter); | ||
} | ||
return filePath; | ||
} | ||
|
||
public static createFilePath(counter: number): string { | ||
return path.join(os.tmpdir(), `SQLQuery${counter}.sql`); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import * as TypeMoq from 'typemoq'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see there's a good number of tests added here, but Coveralls says this change drops code coverage by 32%. That's a huge change. Can we make sure we understand what's going on with coveralls's assessment before we move forward? #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes not sure why. I see a permission error in travis job, might be related In reply to: 94176306 [](ancestors = 94176306) |
||
import vscode = require('vscode'); | ||
import UntitledSqlDocumentService from '../src/controllers/untitledSqlDocumentService'; | ||
import VscodeWrapper from '../src/controllers/vscodeWrapper'; | ||
const fse = require('fs-extra'); | ||
const fs = require('fs'); | ||
|
||
interface IFixture { | ||
openDocResult: Promise<vscode.TextDocument>; | ||
showDocResult: Promise<vscode.TextEditor>; | ||
vscodeWrapper: TypeMoq.Mock<VscodeWrapper>; | ||
service: UntitledSqlDocumentService; | ||
textDocuments: vscode.TextDocument[]; | ||
} | ||
|
||
suite('UntitledSqlDocumentService Tests', () => { | ||
|
||
function createTextDocumentObject(fileName: string = ''): vscode.TextDocument { | ||
return { | ||
uri: undefined, | ||
fileName: fileName, | ||
getText: undefined, | ||
getWordRangeAtPosition: undefined, | ||
isDirty: true, | ||
isUntitled: true, | ||
languageId: 'sql', | ||
lineAt: undefined, | ||
lineCount: undefined, | ||
offsetAt: undefined, | ||
positionAt: undefined, | ||
save: undefined, | ||
validatePosition: undefined, | ||
validateRange: undefined, | ||
version: undefined | ||
}; | ||
} | ||
|
||
function createUntitledSqlDocumentService(fixture: IFixture): IFixture { | ||
let vscodeWrapper: TypeMoq.Mock<VscodeWrapper>; | ||
vscodeWrapper = TypeMoq.Mock.ofType(VscodeWrapper); | ||
|
||
vscodeWrapper.setup(x => x.textDocuments).returns(() => { return fixture.textDocuments; }); | ||
vscodeWrapper.setup(x => x.openTextDocument(TypeMoq.It.isAny())) | ||
.returns(() => { return Promise.resolve(createTextDocumentObject()); }); | ||
vscodeWrapper.setup(x => x.showTextDocument(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())) | ||
.returns(() => { return Promise.resolve(TypeMoq.It.isAny()); }); | ||
fixture.vscodeWrapper = vscodeWrapper; | ||
fixture.service = new UntitledSqlDocumentService(vscodeWrapper.object); | ||
return fixture; | ||
} | ||
|
||
test('newQuery should open a new untitled document and show in new tab' , () => { | ||
let fixture: IFixture = { | ||
openDocResult: Promise.resolve(createTextDocumentObject()), | ||
showDocResult: Promise.resolve(TypeMoq.It.isAny()), | ||
service: undefined, | ||
vscodeWrapper: undefined, | ||
textDocuments: [] | ||
}; | ||
fixture = createUntitledSqlDocumentService(fixture); | ||
|
||
return fixture.service.newQuery().then(result => { | ||
fixture.vscodeWrapper.verify(x => x.openTextDocument( | ||
TypeMoq.It.is<vscode.Uri>(d => d.scheme === 'untitled')), TypeMoq.Times.once()); | ||
fixture.vscodeWrapper.verify(x => x.showTextDocument(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.once()); | ||
}); | ||
}); | ||
|
||
test('newQuery should increment the counter for untitled document if file exits' , () => { | ||
let fixture: IFixture = { | ||
openDocResult: Promise.resolve(createTextDocumentObject()), | ||
showDocResult: Promise.resolve(TypeMoq.It.isAny()), | ||
service: undefined, | ||
vscodeWrapper: undefined, | ||
textDocuments: [] | ||
}; | ||
let counter = getCounterForUntitledFile(1); | ||
fixture = createUntitledSqlDocumentService(fixture); | ||
let filePath = UntitledSqlDocumentService.createFilePath(counter); | ||
if (!fs.existsSync(filePath)) { | ||
fs.writeFileSync(filePath, 'test'); | ||
} | ||
return fixture.service.newQuery().then(result => { | ||
fixture.vscodeWrapper.verify(x => x.openTextDocument( | ||
TypeMoq.It.is<vscode.Uri>(d => verifyDocumentUri(d, counter + 1))), TypeMoq.Times.once()); | ||
fixture.vscodeWrapper.verify(x => x.showTextDocument(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.once()); | ||
if (!fs.existsSync(filePath)) { | ||
fse.remove(filePath, undefined); | ||
} | ||
}); | ||
}); | ||
|
||
function verifyDocumentUri(uri: vscode.Uri, expectedNumber: number): boolean { | ||
return uri.scheme === 'untitled' && uri.path.endsWith(`${expectedNumber}.sql`); | ||
} | ||
|
||
function getCounterForUntitledFile(start: number): number { | ||
let counter = start; | ||
let filePath = UntitledSqlDocumentService.createFilePath(counter); | ||
while (fs.existsSync(filePath)) { | ||
counter++; | ||
filePath = UntitledSqlDocumentService.createFilePath(counter); | ||
} | ||
return counter; | ||
} | ||
|
||
test('newQuery should increment the counter for untitled document given text documents already open with current counter' , () => { | ||
let counter = getCounterForUntitledFile(1); | ||
let fixture: IFixture = { | ||
openDocResult: Promise.resolve(createTextDocumentObject()), | ||
showDocResult: Promise.resolve(TypeMoq.It.isAny()), | ||
service: undefined, | ||
vscodeWrapper: undefined, | ||
textDocuments: [ | ||
createTextDocumentObject(UntitledSqlDocumentService.createFilePath(counter + 1)), | ||
createTextDocumentObject(UntitledSqlDocumentService.createFilePath(counter))] | ||
}; | ||
fixture = createUntitledSqlDocumentService(fixture); | ||
let service = fixture.service; | ||
|
||
return service.newQuery().then(result => { | ||
fixture.vscodeWrapper.verify(x => x.openTextDocument( | ||
TypeMoq.It.is<vscode.Uri>(d => verifyDocumentUri(d, counter + 2))), TypeMoq.Times.once()); | ||
fixture.vscodeWrapper.verify(x => x.showTextDocument(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.once()); | ||
}); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure you explored this already, but I feel like there has to be some command built into VS Code to just create an untitled document. I'm thinking by opening an in-memory untitled doc, the header for the file would be "untitled 1" or some such. Can we verify that there isn't any way to programmatically create an untitled document before we go ahead with this change? #WontFix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that one doesn't let me change the language to sql
In reply to: 94176030 [](ancestors = 94176030)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to change the language of the document after it's created? I took a look at the API but it claims that the languageId of a TextDocument is readonly. I can't really understand why that would be the case, and I'd want further clarification from VS Code folks. #WontFix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that also and it didn't work. I created a feature request in VsCode: microsoft/vscode#17917 (comment)
#WontFix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, seems silly they don't have a programmatic way of doing that, but it's a project that's still growing.